私は Javascript とプログラミング全般に不慣れで、Javascript Enlightenment (p.88)という本からこのコード ブロックにたどり着きました。
var parentFunction = function() {
var foo = 'foo';
return function() { // anonymous function being returned
console.log(foo); // logs 'foo'
}
}
// nestedFunction refers to the nested function returned from parentFunction
var nestedFunction = parentFunction();
nestedFunction(); /* logs foo because the returned function accesses foo
via the scope chain */
設定var nestedFunction = parentFunction();
によりnestedFunction();
、ネストされた匿名関数を呼び出して「foo」をコンソールに記録できるのに、parentFunction();
何もログに記録しないのはなぜですか?