如果不使用闭包变量,是否不创建?

时间:2018-11-07 05:19:34

标签: javascript closures google-chrome-devtools

this example中,Google Chrome Devtools在闭包范围中找不到变量“ y”,但将显示“ x”。

我的猜测是“ y”不会出现,因为它没有被使用。

我对吗?

enter image description here

1 个答案:

答案 0 :(得分:0)

它似乎取决于引擎。

在Google Chrome浏览器(版本70.0.3538.77(正式版本)(64位))上,我看不到“ y”变量:

Chrome

但是在Firefox上可以访问变量“ y”:

Firefox

我不确定这些引擎如何工作,我假设Chrome在编译时查看了变量“ y”,并说“嘿,它不在此范围内,我们不需要它”。 Firefox包含了它。