我「新來的Javascript和編程一般在從一個叫的Javascript啓蒙(第88頁)一書的代碼塊來:調用嵌套匿名函數(JavaScript的)
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();
調用嵌套匿名函數和日誌「富」到控制檯,而只使用parentFunction();
日誌什麼都沒有
爲什麼富可以訪問即使它應該是超出了範圍,因爲JS使用somehing稱爲封:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Closures – HMR