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?
callee is a property of the arguments object. It can be used to refer to the currently executing function inside the function body of that function. This is useful when the name of the function is unknown, such as within a function expression with no name (also called "anonymous functions").
The function. caller property of the JavaScript function object returns the function that invoked the specified function. It returns null if the function “f” was invoked by the top-level code in JavaScript. For functions like strict functions, async functions and generator function this method returns null.
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:
function callback() { // ... setTimeout(callback, 100); } setTimeout(callback, 100);
Which does work on IE.
But what should be then used instead? Something like this?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With