A
alex.malgaroli
Hi all.
I didn't find anything on the web regarding this different behaviour I
found in Firefox (v. 2) and IE (v. 6), so I'm posting it here, maybe
someone will benefit from this.
I have a function in a javascript object like this:
function MyObject.prototype = {
myvar = "This is myvar";
myfunction(myarg) {
// do something with myarg
}
}
If I issue a setTimeout call using a function with some additional
arguments in this form:
setTimeout(this.myfunction, 5, this.myvar);
(for example in another MyObject method), Firefox calls myfunction
passing myvar correctly (as a string valued "This is myvar"). Internet
Explorer seems to pass myvar as undefined.
My idea is that FF evaluates immediately the value to pass to the
function, stores it, and then when the time comes, invokes the
function with the correct value.
Instead it seems (but this is my guess) that IE evaluates the argument
only when it calls the function, thus having lost in the scope the
reference to "this".
I don't have tried it but I think that if I pass global variables or
"local" variables to the setTimeout additional arguments, they work...
Well... I just found this two different doc for setTimeout. I have a
JS reference in PDF (a bit old, referring to 1.3 version) that
complies with the second. This may be the reason why I get
undefined...
http://msdn2.microsoft.com/en-us/library/ms536753.aspx
http://developer.mozilla.org/en/docs/DOM:window.setTimeout#1021427
Just for the sake of completeness: I was using MochiKit so i resolved
using a partial(), when invoking setTimeout thus binding at setTimeout
invoke time the argument values. Worked fine.
I didn't find anything on the web regarding this different behaviour I
found in Firefox (v. 2) and IE (v. 6), so I'm posting it here, maybe
someone will benefit from this.
I have a function in a javascript object like this:
function MyObject.prototype = {
myvar = "This is myvar";
myfunction(myarg) {
// do something with myarg
}
}
If I issue a setTimeout call using a function with some additional
arguments in this form:
setTimeout(this.myfunction, 5, this.myvar);
(for example in another MyObject method), Firefox calls myfunction
passing myvar correctly (as a string valued "This is myvar"). Internet
Explorer seems to pass myvar as undefined.
My idea is that FF evaluates immediately the value to pass to the
function, stores it, and then when the time comes, invokes the
function with the correct value.
Instead it seems (but this is my guess) that IE evaluates the argument
only when it calls the function, thus having lost in the scope the
reference to "this".
I don't have tried it but I think that if I pass global variables or
"local" variables to the setTimeout additional arguments, they work...
Well... I just found this two different doc for setTimeout. I have a
JS reference in PDF (a bit old, referring to 1.3 version) that
complies with the second. This may be the reason why I get
undefined...
http://msdn2.microsoft.com/en-us/library/ms536753.aspx
http://developer.mozilla.org/en/docs/DOM:window.setTimeout#1021427
Just for the sake of completeness: I was using MochiKit so i resolved
using a partial(), when invoking setTimeout thus binding at setTimeout
invoke time the argument values. Worked fine.