creating a copy of a function but with some preset parameters.
The bind() method creates a new function that, when called, has its this keyword set to the provided value, with a given sequence of arguments preceding any provided when the new function is called.REF
bind 複製function,並將其中的 this 設成給定的物件,回傳複製的function
The call() method calls a function with a given this value and arguments provided individually.REF
functionmultiply(a,b){ return a * b; } var multiplyByTwo = multiply.bind(this,2); // set first parameter(a) = 2, reutrn a new copy of multiply function console.log(multiplyByTwo(4));
why whattosay is still exist when invoking sayHi ?
greet function is pop off the execution stack, so whattosay should be clear.
Closures: closing in all variables that the function supposed to have access to
the execution context has closed in its outer environment reference(only variables, not values), even though those outer execution contexts are gone.
1 2 3 4 5 6 7 8 9
functiongreet(whattosay){ returnfunction(name){ console.log(whattosay + ', ' + name); } } greet('Hey')('Necisam');//Hey, Necisam var sayHi = greet('Hey');// a function sayHi('Necisam');
4-47 Understanding Closures part2
Closures close only the variables, not the values.
Free variables : it is outside a function, but that you have access to.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
functionbuildFunctions(){ var arr = []; for(var i = 0; i < 3; i++){ arr.push( function(){ console.log(i); } ); } return arr; } var fs = buildFunctions(); fs[0](); fs[1](); fs[2](); // 3 3 3 // i is set to 3 after the loop //
to execute the functions to get different execution context that contains different J s.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
functionbuildFunctions(){ var arr = []; for(var i = 0; i < 3; i++){ arr.push( (function(j){ returnconsole.log(j); }(i)) ); } return arr; } var fs = buildFunctions(); fs[0](); fs[1](); fs[2]();
ES6 solution
let allows you to declare variables that are limited in scope to the block, statement, or expression on which it is used.
each j would be a new variables in mem(just like c language)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
functionbuildFunctions(){ var arr = []; for(var i = 0; i < 3; i++){ let j = i; arr.push( function(){ console.log(j); } ); } return arr; } var fs = buildFunctions(); fs[0](); fs[1](); fs[2]();
4-48 Funciton factories
Every time the function be invoked, a new execution context is created.
This lets us create functions from other functions.
//function express, return a object to var var anonymousGreet = function (){ console.log('hi'); } anonymousGreet(); //use () to invoke a function
function hoisting
1 2 3 4 5
anonymousGreet(); //error : anonymousGreet is undefined this line var anonymousGreet = function (){ //anonymousGreet is set to a function console.log('hi'); }
pass function as parameter
1 2 3 4 5 6 7 8
functionlog(a){ console.log(a); }
log(function (){ console.log('hi'); });
#4-36 By value and By reference
In js, primitives are by value
In js, objects(includes functions) are by reference
this.name = "updated c object"; console.log(this);//c object
var setname = function(newname){ this.name = newname;// the global object ,windows!!! // a lot of people think it's wrong } setname('updated again!!'); console.log(this); //c object with name : "updated c object" } } c.log();
tips avoiding weird this
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
var c = { name:"the c object", log: function(){ var self = this; //save this(c object) address in var self
self.name = "updated c object"; console.log(self);
var setname = function(newname){ self.name = newname;// self point to the c objects } setname('updated again!!'); console.log(self); } } c.log();
4-38 Array-collection of anything
In javascript, array is typeless. You can put anything into it(Even a function = special object).
array contains everything
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
var arr = [ 1, true, { name:'sam', phone:'0911111111' }, function (name){ var greeting = 'Hello '; console.log(greeting + name); }, "bello" ] arr[3](arr[2].name); // "Hello sam"
4-39 arguments and spreads
arguments keyword contains all the function arguments
arguments is array-like, not exactly regular array