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

To perform actions such as

setTimeout(function () { ... setTimeout(arguments.callee, 100); }, 100); 

I need something like arguments.callee . I found information on javascript.info that arguments.callee deprecated:

This property is deprecated by ECMA-262 in favor of a named expression function and for better performance.

But what should be used instead? Something like that?

 setTimeout(function myhandler() { ... setTimeout(myhandler, 100); }, 100); // has a big advantage that myhandler cannot be seen here!!! // so it doesn't spoil namespace 

BTW, cross-browser compatible with arguments.callee ?

+33
javascript cross-browser
Dec 02 '11 at 19:38
source share
3 answers

Yes, that is what should theoretically be used. You're right. However, it does not work in some versions of Internet Explorer, as always. So be careful. You may need to return to arguments.callee or rather simply:

 function callback() { // ... setTimeout(callback, 100); } setTimeout(callback, 100); 

What works in IE.

+10
Dec 03 '11 at 2:15
source share

But what should be used instead? Something like that?

Yes, you answered your question. For more information see here:

Why is the arguments.callee.caller property deprecated in JavaScript?

It pretty well discusses why this change was made.

+5
Dec 02 2018-11-12T00:
source share

Minitech's answer is not bad, but it lacks another scenario. Your declaration function is called a callback, which means two things: first, the function is an object in memory, and the second is the name of the function only to refer to the object. If for any reason you break the link between the two, the proposed code will not work either.

Evidence:

 function callback() { // ... setTimeout(callback, 100); } setTimeout(callback, 100); var callback2 = callback; //another reference to the same object callback = null; //break the first reference callback2(); //callback in setTimeout now is null. 

From the Mozilla developer page in description:

Warning. The fifth edition of ECMAScript (ES5) prohibits the use of arguments.callee () in strict mode. Avoid using arguments.callee () either by giving functional expressions to the name, or by using a function declaration in which the function must call itself.

Obviously, this is the first example of a workaround, โ€œgiving the function expressions a name,โ€ but let's see how we can deal with โ€œor use a function declaration where the function should call itselfโ€ and what it will bring:

 function callback(){ //... setTimeout(innercall(), 100); function innercall(){ //innercall is safe to use in callback context innercall.caller(); //this will call callback(); } } 

Then we can do whatever we want with a callback link:

 var callback2 = callback; callback = null; callback2(); //will work perfectly. 
0
Jul 09 '15 at 21:25
source share



All Articles