For doing things like
setTimeout(function () {
...
setTimeout(arguments.callee, 100);
}, 100);
I need something like arguments.callee
. I found information at javascript.info that arguments.callee
is deprecated:
This property is deprecated by ECMA-262 in favor of named function expressions and for better performance.
But what should be then used instead? Something like this?
setTimeout(function myhandler() {
...
setTimeout(myhandler, 100);
}, 100);
// has a big advantage that myhandler cannot be seen here!!!
// so it doesn't spoil namespace
BTW, is arguments.callee
cross-browser compatible?
Yes, that's what, theoretically, should be used. You're right. However, it doesn't work in some versions of Internet Explorer, as always. So be careful. You may need to fall back on
arguments.callee
, or, rather, a simple:Which does work on IE.
minitech answer is quite good, but it is missing one more scenario. Your declare function called callback, which means two things, first the function is object in memory, and the second, the function name is only for referencing to the object. If you, for any reason break the reference between these two, the proposed code will not work too.
Proof:
From developer Mozilla page in the description is:
obviously this is the first example of workaround "by either giving function expressions a name", but lets see how we can deal with "or use a function declaration where a function must call itself" and what will that bring:
Then we are safe to do whatever we want with the callback reference:
Yes, you answered your own question. For more information, see here:
Why was the arguments.callee.caller property deprecated in JavaScript?
It has a pretty good discussion about why this change was made.