在立即执行函数中直接执行show,chrome显示为闭包,而show是在词法作用域里面执行的,好像有在词法作用域外面调用才会形成闭包,不知道我的理解对不对?比如下面的执行:
是chrome不准确吗?要以什么为标准?
你不知道的JavaScript地57页是这么定义闭包的:
当函数可以记住并访问所在的词法作用域, 即使函数是在当前词法作用域之外执行, 这时就产生了闭包。
你不知道的JavaScript代码:
解释:
那就是这本书和chrome的结果是不一样的。
function foo() {
var a = 2;
function bar() {
a++;
console.log(a);
}
bar();//3
bar();//4
}
foo();
按照书中的说法这和按照词法作用域查找没啥区别。对原著作者提问,他的回答:
Academically it's called a closure... but observationally it's not different from lexical scope lookup. closure is the ability for that lexical scope to be preserved even when the function runs elsewhere. I define closure not by academic technicalities but by pragmatic observations. "scope & closures": ch5,'Nitty Gritty' example is diffrent from chrome
你体会下这句话