Forgotten variables issue in Firefox?

2 views
Skip to first unread message

Brett Zamir

unread,
Dec 30, 2009, 2:48:58 AM12/30/09
to same...@googlegroups.com
Since xmpp4moz uses a lot of handlers or objects being passed around, I
was wondering if anyone else has encountered Firefox occasionally
apparently forgetting variables which were in its scope at the time of
function definition? I'm not encountering this in xmpp4moz now, but
thought I'd ask here before fishing around Firefox lists, since I'd
imagine users here might have encountered this...

Do object references (as opposed to copies) ever "expire" if they are
passed around too much (e.g., if called/applied in a different 'this'
scope)? I really don't think I'm making copies so I don't see how this
information could get lost, and I have seen this work a lot of the time,
but it periodically does give me errors about such variables being
undefined...

For example, I'm adding observers like:

var debug = function (m) {alert(m);};

obj.addObservers({
action1 : function () {
debug('warning'); // Debug should be in scope but I sometimes
get errors saying debug is undefined
}
action2 : function () {
debug('warning 2');
}
});

(Btw, addObservers() in addition to storing the functions in arrays
according to the action topic name, will also keep a reference to the
notifying object so that the specific observer functions will later be
run in that object's scope. Does my problem possibly have anything to do
with references being what I have seen called "weak", and if so, how
might I overcome them being treated this way?)

thanks,
Brett


Marco Pivetta

unread,
Dec 30, 2009, 3:34:55 AM12/30/09
to same...@googlegroups.com
Since Javascript 2 is science-fiction for now, I could suggest you tu place all your libraries inside some kind of "package".
Just define

 MyLibObj = {
  mycustomfunc : function () { /*blah*/ },
  debug : function (m) { alert(m); }
 }


That way your "debug" function will not be overwritten, as it is too difficult to find a software that uses and defines another time "MyLibObj".
This way you also avoid scope pollution :)
If you really made some function called "debug" it's easy to get in conflict with other software using a function/var called the same way...
Just place MyLibObj in your window scope and you can use it almost anywhere :)

2009/12/30 Brett Zamir <bre...@gmail.com>



--
Marco Pivetta - Ocramius Aethril
Standard Ogame Project - StOgame
http://www.stogame.net
Making Ogame a better place...

Brett Zamir

unread,
Dec 30, 2009, 5:09:32 AM12/30/09
to same...@googlegroups.com, Marco Pivetta
Hi,

Thank you for your reply. My replies below...


On 12/30/2009 4:34 PM, Marco Pivetta wrote:
Since Javascript 2 is science-fiction for now, I could suggest you tu place all your libraries inside some kind of "package".
Just define

 MyLibObj = {
  mycustomfunc : function () { /*blah*/ },
  debug : function (m) { alert(m); }
 }


That way your "debug" function will not be overwritten, as it is too difficult to find a software that uses and defines another time "MyLibObj".
This way you also avoid scope pollution :)

Yes. I'm not using this in an overlay though, so it is just my extension. I generally avoid globals normally.

But in any case, my problem remains of the object being referenced somehow disappearing sometimes...

I might have mentioned too that I'm importing "debug" from a JavaScript module into the global scope (or rather, I'm using a loader to load a script into the global scope which in turn both imports a JavaScript module into the global scope and (then) adds the observers). Maybe there's a problem in there...

Brett
Reply all
Reply to author
Forward
0 new messages