22
loading...
This website collects cookies to deliver better user experience
function square(x) {
return x * x
}
// Assigned to another variable
let f = square
console.log(square(5))
console.log(f(5))
// sqr the the passed square function
function my_map(sqr, args) {
let result = []
for (let i = 0; i < args.length; i++) {
// the passed function is used here
result.push(sqr(args[i]))
}
return result;
}
// square function is passed as argument
let squares = my_map(square, [1, 2, 3, 4, 5, 6, 7])
console.log(squares)
function logger(msg) {
function log_message() {
console.log("Log : " + msg)
}
// this is a function, returning from parent functions
return log_message
}
logHello = logger("hello")
logHello()
function outer_function() {
message = "hello world"
function inner_function() {
console.log (message) // Look at point 2 from definition
}
return inner_function()
}
// invoked from outside
outer_function()
function outer_function(msg) {
message = msg
function inner_function() {
console.log (message)
}
return inner_function
}
let func = outer_function("Hello World")
func()
// strings in js are immutable
// they cannot be changed once initialised
let name = "uday Yadav"
name[0] = "U";
// this makes not difference
console.log(name);
// still small case 'u'
console.log(name[0]);
// array in js is mutable
// they can be changed once created
let data = [0,2,3,4];
data[0] = 1;
console.log(data);
let cache = {}
function expensive_compute(data) {
if (cache.hasOwnProperty(data)) {
console.log("answer cache : "+cache[data])
cache[data] = data*data
return;
}
cache[data] = data*data
console.log("answer : "+cache[data])
}
expensive_compute(4)
expensive_compute(10)
expensive_compute(4)
expensive_compute(16)
expensive_compute(10)
log(Car.state) // unlocked
Remote.lock();
log(Car.state) // locked
Remote.lock();
Remote.lock();
Remote.lock();
log(Car.state) // locked)
lock
is an idempotent operation. Even if there are some side effect each time you run lock, like blinking, the car is still in the same locked state, no matter how many times you run lock operation.
NON-IDEMPOTENT: If an operation always causes a change in state, like POSTing the same message to a user over and over, resulting in a new message sent and stored in the database every time, we say that the operation is NON-IDEMPOTENT.
NULLIPOTENT: If an operation has no side effects, like purely displaying information on a web page without any change in a database (in other words you are only reading the database), we say the operation is NULLIPOTENT. All GETs should be nullipotent.
let arr = [1, 2, 3];
let mapped = arr.map(x => Math.pow(x, 2));
// x => is a function without a name
console.log(mapped);
class Animal {
constructor(_type) {
this.type = _type;
}
}
function makeSound(animal) {
if (isCat(animal)) {
console.log(" MEOW ! ");
return;
}
console.log(" NOT CAT ! ");
}
function isCat(animal) {
return animal.type === 'Cat';
}
let newCat = new Animal('Cat');
makeSound(newCat);
let data = {
"name": "Uday Yadav",
"Gender": "Male"
}
let str = JSON.stringify(data)
console.log(str + "|" + typeof str)
let dataReturns = JSON.parse(str)
console.log(dataReturns + "|" + typeof dataReturns)