:
var a = 100;
function afunc(infunc){
a = 10;
var f = function (){
console.log(a);
};
f.call();
infunc.call();
}
afunc(function (){ console.log(a); });
10 a afunc, , a. , 10 a, .
infunc , , afunc, , . a , a infunc.
, :
function bfunc(){
var b = 'hello';
return function (){ console.log(b); };
}
afunc(bfunc());
Here, the anonymous function returned bfuncis actually called from the region afunc, but it is still able to register the correct value b, since it was assigned in the original region. If you had to change afuncto assign a different value b, it would still write "hello", because the bone defined in afuncis a different variable than the bone defined in bfunc.
source
share