views:

86

answers:

2

I'm setting a breakpoint in the code below where it says "breakpoint". Also adding a watch expression for dataStore.

function(){
  var self = {};
  var dataStore = [];
  var areEq = UNAB.objectsAreEqual;

  self.put = function(key, value){
    /*breakpoint*/ dataStore.push({key:key, value:value});
  }
  return self;
}

At this breakpoint, Firebug tells me "ReferenceError: dataStore is not defined". Same results with trying to examine "areEq". However, dataStore.push executes without error. An additional strangness: adding a watch expression for "self" shows not the self object I expect, with one property, "put", but the "window" object.

Any idea what the heck is going on?

+1  A: 

Probably self is getting resolved by Firebug and probably also by Firefox in the global scope as referring to the current window. If you choose a different name other than "self", your code should make everyone happy.

Ishmael
You're correct. `window.self` is the same as `window`. The OP's code itself is perfectly correct JavaScript and will execute without error, but it seems the Firebug debugger somehow has the wrong context when it breaks.
casablanca
I've been creating "classes", using var self-{} for over a year, many dozens of times, and never had a problem with self. I'll try something else though. Thanks!
morgancodes
I replaced "self" with "_this", and get the same behavior. Annoying! Weird!
morgancodes
+1  A: 

I think this is a firefox bug. If you set a breakpoint on var dataStore = []; then continue, when hit the breakpoint in put(). you get a closure scope (in Firebug 1.6). That scope has dataStore and self. I think Firefox is optimizing the closure scope away, perhaps since the code is nonsense anyway: there is no way to access dataStore.

A complete test case will eventually appear at http://getfirebug.com/tests/script/stackoverflow/dataStoreIsNotDefined.html

see also https://developer.mozilla.org/en/DOM/window.self

johnjbarton
Thanks for confirming that I'm not crazy (at least not in this particular way). And thanks for reporting the bug.
morgancodes