Javascript Arguments.callee 已弃用 - 应该使用什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8361642/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 05:41:16  来源:igfitidea点击:

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

javascriptcross-browser

提问by TMS

For doing things like

对于做类似的事情

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

I need something like arguments.callee. I found information at javascript.infothat arguments.calleeis deprecated:

我需要类似的东西arguments.callee。我在 javascript.info上找到了arguments.callee已弃用的信息:

This property is deprecatedby ECMA-262 in favor of named function expressions and for better performance.

ECMA-262不推荐使用此属性,以支持命名函数表达式并提高性能。

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.calleecross-browser compatible?

顺便说一句,arguments.callee跨浏览器兼容吗?

采纳答案by Ry-

Yes, that's what, theoretically, shouldbe 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:

是的,这就是理论上应该使用的。你是对的。但是,它一如既往地不适用于某些版本的 Internet Explorer。所以要小心。您可能需要依靠arguments.callee,或者更确切地说,是一个简单的:

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

setTimeout(callback, 100);

Which does work on IE.

哪个在 IE 上工作。

回答by Jonathan Rich

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?

为什么 JavaScript 中不推荐使用 arguments.callee.caller 属性?

It has a pretty good discussion about why this change was made.

关于为什么要进行此更改,它有一个很好的讨论。

回答by lowselfesteemsucks

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.

minitech 的答案非常好,但它缺少另一种情况。你声明的函数叫回调,这意味着两件事,第一,函数是内存中的对象,第二,函数名只用于引用对象。如果您出于任何原因破坏了这两者之间的引用,则建议的代码也将不起作用。

Proof:

证明:

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 developer Mozilla pagein the description is:

来自开发者 Mozilla 页面的描述是:

Warning: The 5th edition of ECMAScript (ES5) forbids use of arguments.callee() in strict mode. Avoid using arguments.callee() by either giving function expressions a name or use a function declaration where a function must call itself.

警告:ECMAScript (ES5) 第 5 版禁止在严格模式下使用 arguments.callee()。避免使用arguments.callee() 通过给函数表达式一个名称或在函数必须调用自身的地方使用函数声明。

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:

显然,这是第一个解决方法“通过给函数表达式命名”的示例,但让我们看看我们如何处理“或使用函数声明,其中函数必须调用自身”以及这会带来什么:

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

Then we are safe to do whatever we want with the callback reference:

然后我们可以安全地使用回调引用做任何我们想做的事情:

var callback2 = callback;
callback = null;
callback2(); //will work perfectly.