Chrome debugger: unused variables not loaded in JavaScript closure

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

Obviously, when you create a closure, the JavaScript engine does not preserve all scope variables. It saves only those that are actually used by the internal function. This leads to incorrect results in the debugger if you stop 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()() 

Note that only id2 is defined in the close area: Only id2 is defined in the closure scope

2.Remove the console.log statement.

There are now two variables in the closing area There are two variables in the closure scope

Does anyone know why this is happening (I believe this is a memory), and are there other aspects of this that we should be aware of.

+5
source share

Source: https://habr.com/ru/post/1206334/


All Articles