The variable q inside the function f is local, i.e. available inside the body function. Question: why is it not available in the obj.func function?

 var qwe = 1;
 function f () {
     var q = {};
     var qwe = 2;
     debugger;
     var obj = {
         a: q,
         func: function () {
             // here q is not visible, but qwe === 1.
             debugger;
         }
     };
     obj.func ();
     debugger;
 }

 f ();

It seems like when creating obj.func, its [[Scope]] should refer to the variable object of the function f (since func was created in the context of the function f), but from the code you can see that it refers to the global object (The qwe variable is 1 , not 2).

  • A similar question was already the other day - nick_n_a
  • For some reason, obj.func is not considered to be nested with respect to f. - bulatov
  • Very similar to the debugger bug. Chrome and ie bazhat, and in different ways. ff - okay. - Duck Learns to Take Cover

3 answers 3

In fact, the test is somewhat incorrect.

The debugger shows the real state of the Osprey.

Example in chrome:

in chrome

As you can see at the time of the function call, two ospes are available:

  1. Local Skop Function
  2. Global Osp.

The incorrectness of the test is that the function is empty, there are no accesses to variables from the function group f , respectively, there are no closures.

When checking: the search is carried out in the currently accessible ospses, and there is really no q variable, and the qwe variable is in the global osprey and has the value 1.


If you change the code by adding a closure to it. A completely different picture will appear:

chrome with closure

As it can be noted, the Closure Cluster was added, in which is used the variable q from the Osprey f .

And when checking - it will be defined and have a value - an empty object.

It is also worth noting that since the qwe variable is not closed, it will still give 1 when checked.

    Let's not take the word for anyone, but simply run the code.

     var qwe = 1; function f() { var q = { test: "Test" }; var qwe = 2; var obj = { a: q, func: function() { // тут q не видна, а qwe === 1. console.log(q); // видна console.log(qwe); // 2 } }; obj.func(); } f(); 

    • one
      Nene, run the author code and look in the debugger. Apparently the problem is that the debugger is lying (at least in my chrome - it is lying o_O) - Duck Learns to Take Cover
    • Those. the question is essentially not "why the hell js behaves like that" but "why the hell debugger behaves like that" - Duck Learns to Take Cover
    • @ Moderately Upset Chrome Version 54.0.2840.99 m - debugger shows correctly, and you? - Igor
    • 55.0.2883.87 m - Chrome debugger bazhit like the author - Duck Learns to Take Cover
    • 11 donkey - also bazhit. But in ff - norms. - Duck Learns to Take Cover

    Variables have been removed by the garbage collector ( Optimization in V8 and its consequences ).

    • they have not been deleted. Since they are available after calling the function. - Grundy
    • link is not very explanation - Grundy