5

Came across an interesting issue with Google Chrome's debugger today. A variable looked like it wasn't available, but it was - just not available to the debugger. Here's some example code:

<script>

var foo = function(passedInThing, otherPassedInThing) {
  return {
    bar: function() {
      window.im_using = passedInThing;
      debugger;
    }
  }
}

foo("this exists", "this does not").bar();

</script>

Because I've made a reference to passedInThing within the scope of the inner function, the debugger can see that variable and manipulate it. But otherPassedInThing seems to be invisible until it is used.

As far as I can tell, firefox doesn't seem to have the same limitation. Is this a bug, or is there some rationale behind this?

user208769
  • 2,216
  • 1
  • 18
  • 27
  • 2
    I suspect that's not a bug, more likely a memory optimization as the variable's not used in the closure so why pass it in. It's probably a difference in the javascript engines of the browsers. – mattmanser Apr 02 '14 at 11:42
  • 2
    Chrome's JavaScript engine is amazingly good at optimizing out anything it doesn't need. Call it a feature or a bug, but I'm betting on the fact that Chrome didn't see any use for `otherPassedInThing` and simply omitted it from the compiled code. – Cᴏʀʏ Apr 03 '14 at 11:33
  • 3
    this is kind of silly. if the debugger is up it shouldn't optimize. i mean, it's like compiling `-g` without `-Ox` and then finding the compiler did optimization. it's also annoying to have to go back and add a console.log or other nonsense just to be able to see the variable (and hopefully the problem is easy to reproduce!) – Michael Jun 03 '14 at 17:59
  • 3
    I thought I was going insane when I found this. Thanks for restoring my sanity guys. – tobek Dec 23 '14 at 20:02

0 Answers0