Chrome Debugger: Unused variables not loaded in a JavaScript Closure [closed]

荒凉一梦 提交于 2019-12-10 13:30:09

问题


Not a problem, because I found out what causes it, but a huge quirk nevertheless:

Apparently when you create a closure, the JavaScript engine does not save all of its scope variables. It saves only the ones that are really used by the inner function. This causes incorrect results in the debugger if you halt your program. Here is how you can reproduce this

1.Run the following snippet in Chrome:

function foo (){
    var id = 0
    var id2 = 1
    return function foo2(){
        //console.log(id)
        console.log(id2)
        debugger
    }
}

foo()()

Notice that only id2 is defined in the closure scope:

2.Uncomment the console.log statement.

There are now two variables in the closure scope

Does someone know why this happens (I presume it is to save memory) and are there any other aspects of this that we should be aware of.

来源:https://stackoverflow.com/questions/26776876/chrome-debugger-unused-variables-not-loaded-in-a-javascript-closure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!