9

When I try to debug this code (http://jsfiddle.net/QWFGN/)

var foo = (function(numb) {
    return {
        bar: function() {
            debugger;
            return "something";
        }
    }
})(1);
foo.bar()

Developer tool in Chrome behaves differently than and Firebug in Firefox and developer tool in IE. The issue is that variable numb is not visible in Chrome developer tool on the debugger; line. But, it is visible in Firebug and IE. If I try to type numb in Chrome's console I get:

ReferenceError: numb is not defined

numb, of course, is visible in this closure, and if I change code to (http://jsfiddle.net/QWFGN/1/)

var foo = (function(numb) {
    return {
        bar: function() {
            debugger;
            console.log(numb);
            return "something";
        }
    }
})(1);
foo.bar()

numb is now visible in Chrome as well and I can get value 1 as a response.

So, My question is: Why only Google Chrome doesn't see closure variables that are never used? Does Google Chrome have it's own implementation of Garbage Collection, or is it only related to implementation of debug tool in Google Chrome.

Matt Cain
  • 5,638
  • 3
  • 36
  • 45
dugokontov
  • 4,402
  • 1
  • 25
  • 25
  • possible duplicate of [About closure, LexicalEnvironment and GC](http://stackoverflow.com/questions/8665781/about-closure-lexicalenvironment-and-gc) or [How are closures and scopes represented at run time in JavaScript](http://stackoverflow.com/questions/5368048/how-are-closures-and-scopes-represented-at-run-time-in-javascript) – Bergi Mar 14 '13 at 13:46

1 Answers1

8

This doesn't have anything to do with garbage collection or the debug tools.

What's actually happening is that Chrome's JS engine realizes that you never use numb inside the function, so it doesn't include it in the closure at all.

Note that it can only do this if it can prove that the inner function never uses with or calls eval.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • 2
    Hi, beg your pardon for asking, but could you add a link to some official source about this? It would be great! – Sebas Mar 14 '13 at 13:42
  • Obviously @SLaks is right, it is hard to pin point this in V8 documentation. Similar discussion coming to same conclusion -> http://comments.gmane.org/gmane.comp.lang.javascript.v8.general/678 – Haris Krajina Mar 14 '13 at 13:53
  • Thx for answer. Here are some references I have found: http://dmitrysoshnikov.com/ecmascript/es5-chapter-3-2-lexical-environments-ecmascript-implementation/#codeevalcode-and-inner-functions-may-break-optimizations, https://twitter.com/erikcorry/status/53901976865476608. If someone finds link to V8 documentation where this is specified, I would really appreciate. – dugokontov Mar 14 '13 at 13:58
  • 1
    @dugokontov there is no documentation for this, as this is an implementation detail. You can read scope allocation code to see what really happens: https://code.google.com/p/v8/source/browse/branches/bleeding_edge/src/scopes.cc?r=13781#1179 – Vyacheslav Egorov Mar 14 '13 at 14:09