I've always found this bit of code useful: var rescope = function(scope, method){ if(!method){ method = scope; scope = null; } if(typeof method == "string"){ scope = scope || window; if(!scope[method]){ throw(['method not found']); } return function(){ return scope[method].apply(scope, arguments || []); }; } return !scope ? method : function(){ return method.apply(scope, arguments || []); }; }
An example: var myObj = { interval: 1000, count: 0, start: function(){ if(this.timer){ return; } this.timer = setInterval(rescope(this, "update"), this.interval); }, stop: function(){ clearInterval(this.timer); delete this.timer; }, update: function(){ console.log(++this.count); } }; myObj.start(); Regards, Peter Higgins On Fri, Jan 2, 2009 at 5:49 PM, Michael Geary <m...@mg.to> wrote: > > Hi Alexandre, > > Don't go adopting a coding practice just because of a single mailing list > message. :-) > > There's nothing wrong with quoting property names in an object literal, but > the majority of experienced JavaScript programmers do not quote them except > when necessary. As an example, browse through the jQuery source code: > > http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.js > > Most of the property names in the code are not quoted, except for those few > that are invalid identifiers or reserved words. > > Regarding setTimeout and setInterval, a minor nitpick on terminology: > "setTimeout and setInterval scope the called function to the window object". > Actually, the *scope* of the called function is determined by its position > in the source code. JavaScript uses lexical scoping, where a nested function > can directly refer to variables declared in outer functions or in the global > scope. That's why your setInterval callback is able to use your thisObj > variable, because the interpreter follows the scope chain from the inner > function up to the outer function and finds the variable there. > > What you're talking about is the value of "this" in the setInterval > callback. setInterval and setTimeout call your callback function as a method > of the global object (which is the window object in a browser). Or another > way to put it is that they don't call the function as a method of any object > at all, and by default "this" is set to the global object. > > In any case, terminology nitpicks aside, your code is the right way to solve > the problem! :-) > > -Mike > >> From: Alexandre Plennevaux >> >> hi donb, according to a lengthy discussion we had on this >> mailinglist yesterday the quotes are good practice. see: >> http://groups.google.com/group/jquery-en/msg/821f4eb134c51d3d >> (is is just one message on a 31-long thread, if u have time >> ,read the whole thread it is interesting ) >> >> As for this issue after extensive googling i found out that >> setTimeout and setInterval scope the called function to the >> window object, not the object the setinterval is called in. >> >> Therefore here is how to do it: >> >> var datascape = { >> 'mouseX': 0, >> 'myInterval': 0, >> 'create': function(){ >> >> var thisObj = this; //<-- store this object instance in a variable >> >> $('#datascape').bind('mousemove', function(e) >> { >> this.mouseX = e.pageX; >> }).bind("mouseover", function() >> { >> datascape.myInterval = setInterval(function() { >> thisObj.move(); }, 1000); // <-- use the vairable >> referencing the instance >> >> }).bind("mouseout", function() >> { >> clearInterval(datascape.myInterval); >> }); >> }, >> >> 'move': function(){ >> $.log('datascape.move : mouseX = ' + this.mouseX); >> } >> } >> >> On Thu, Jan 1, 2009 at 3:46 PM, donb >> <falconwatc...@comcast.net> wrote: >> > >> > You should change 'move' to move (remove apostrophes). >> > >> > >> > On Jan 1, 9:01 am, "Alexandre Plennevaux" <aplennev...@gmail.com> >> > wrote: >> >> Hello mates, >> >> >> >> i have an object datascape which among other things, contains a >> >> property storing the mouse position, and a function that uses that >> >> property. Inside another method i >> >> >> >> var datascape = { >> >> 'mouseX': 0, >> >> 'myInterval': 0, >> >> 'create': function(){ >> >> $('#datascape').bind('mousemove', function(e) >> >> { >> >> this.mouseX = e.pageX; >> >> }).bind("mouseover", function() >> >> { >> >> this.myInterval = setInterval(this.move, 200); >> >> >> >> }).bind("mouseout", function() >> >> { >> >> clearInterval(this.myInterval); >> >> }); >> >> }, >> >> >> >> 'move': function(){ >> >> $.log('datascape.move : mouseX = ' + this.mouseX); >> >> } >> >> >> >> } >> >> >> >> Yet the script does not work: >> >> firebug console points at the setInterval call, saying: >> >> >> >> useless setInterval call (missing quotes around >> >> argument?)http://localhost/prototype/_js/frontend/proto.03.js >> >> Line 172 >> >> >> >> can someone help me / explain what i'm doing wrong ? >> >> >> >> Thank you, >> >> >> >> Alexandre >> >> >> >> PS: to all jquerians: happy 2009 !! >> > >