4

I run the snippets in chrome and watch the variables.
However, something puzzled me: the variables a,b,c are all declared in the closure scope, but a and b are not available. Please make clear why this happens?

watch variable in chrome dev tool

The offcial Doc doesn't make it clear.

I make an assumption:“The variable in chrome's watch expression must be used in the local scope or it's declared in the global scope, otherwise it's not available even if it has been declared in closure scope”. It's the proposition correct?

Chrome version:60.0.3

The following is my code for you to test:

var globalV = 123;
;(function(){
var a = function(){}
var b = function(){a()}
var c = function(){b()}
var d = function(){
    debugger
    c()
  }
d()
})()
Ye Shiqing
  • 1,519
  • 2
  • 15
  • 24

2 Answers2

1

Looks like it may be a bug. I created an issue so that someone on the DevTools team will look at it: https://crbug.com/762265

Kayce Basques
  • 23,849
  • 11
  • 86
  • 120
0

To me it looks like a reasonable optimization - why include unused variables to a closure in runtime.

I mean, they could be included, but the interpreter/compiler is to include only the used ones - and the debugger uses the same principle.

For example, in C# (.NET Core 2.0) it works the same way: enter image description here

Dmitry Karpenko
  • 544
  • 5
  • 6