Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Arguments.callee is deprecated - what should be used instead?

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?

like image 685
Tomas Avatar asked Dec 02 '11 19:12

Tomas


People also ask

What is arguments callee name?

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").

What is function caller in JavaScript?

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.


2 Answers

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.

like image 131
Ry- Avatar answered Oct 09 '22 14:10

Ry-


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.

like image 27
Jonathan Rich Avatar answered Oct 09 '22 14:10

Jonathan Rich