Closures
12345678910111213141516171819202122232425262728293031323334
/*
Closure functions are functions that refer to the state of their enclosing
function's scope (this includes the 'this' keyword). Any free variable in the
closure function that is declared in the outer function's scope or received as
a parameter.
*/
function parent(param) {
return function () {
console.log(param);
};
}
var a = parent('here');
a();
/*
This behavior can also be chained.
*/
function parent_2(param) {
function getter() {
return function () {
console.log(param);
};
}
return getter;
}
var b = parent_2(2);
var c = b();
c();
JavaScript
INFO