The function greaterThanreturns the function when called. The returned function has access to all members of the external function even after the function returns. This is called closure.
function greaterThan(n) {
return function (m) {
return m > n;
};
}
When following the instructions
var greaterThan10 = greaterThan(10);
it is converted as
var greaterThan10 = function (m) {
return m > 10;
};
So, greaterThan10now it is a function and can be called as
console.log(greaterThan10(11));
m 11, return 11 > 10; true.
:
JavaScript?
, JS
http://dmitryfrank.com/articles/js_closures