简体   繁体   English

Javascript arguments.callee它的用途是什么

[英]Javascript arguments.callee what is it for

I haven't found any complete cross-browser doc of this variable. 我还没有找到这个变量的任何完整的跨浏览器文档。

What is arguments.callee for? arguments.callee是什么? how does it work? 它是如何工作的?

Which arguments does it have? 它有哪些论据?

arguments.callee is a reference to the function that is currently being called. arguments.callee是对当前正在调用的函数的引用。 First things first: don't use it: if you're in a strict context, it'll just spew errors. 首先要做的事情是:不要使用它:如果你处于严格的环境中,它只会出现错误。

However, personally -and I'm not alone in this - I'll miss this property. 但是,亲自 - 而且我并不孤单 - 我会想念这家酒店。 Before I get to explain why, I'll give you a pseudo-example of when you might use this: 在我解释原因之前,我会给你一个伪示例,说明何时使用它:

var looper = (function(someClosureVar)
{
    setTimeout((function(resetTimeout)
    {
        return function()
        {
            //do stuff, stop OR:
            resetTimeout();
        };
    }(arguments.callee)),1000);
}(document.getElementById('foobar')));

I hope you like closures, because I do - and that's where arguments.callee are very likely to occur. 我希望你喜欢闭包,因为我这样做 - 而且这就是很有可能发生arguments.callee的地方。 The next-to-last line is where the money is: 倒数第二行是钱的位置:

(arguments.callee)

Is a reference to the anonymous function that sets the initial timeout, within a closure scope (that has access to 1 DOM element, in this case). 是对闭包范围内设置初始超时的匿名函数的引用(在这种情况下可以访问1个DOM元素)。 Anonymous functions are GC'ed after they return, but in this case, I've added it to the timeout callback's scope (passed it as an argument to another anonymous function that returns the actual callback), so it is still referenced somewhere. 匿名函数在返回后是GC'ed,但在这种情况下,我已将它添加到超时回调的作用域(将其作为参数传递给另一个返回实际回调的匿名函数),因此它仍被引用到某处。
Now, if you're in strict you needn't worry because this is what the code would look like in strict mode: 现在,如果你是严格的,你不必担心,因为这是代码在严格模式下的样子:

var looper = (function tempName(someClosureVar)
{
    setTimeout((function(resetTimeout)
    {
        return function()
        {
            //do stuff, stop OR:
            resetTimeout();
        };
    }(tempName)),1000);
}(document.getElementById('foobar')));

Name the function and that's it. 命名函数,就是这样。 Why don't I like it? 我为什么不喜欢它? arguments.callee raises flags, just like anonymous functions that some closure trickery is going on. arguments.callee引发了标志,就像一些封闭技巧正在进行的匿名函数一样。 I guess it's just a habit, but its one that, I feel, helps me to structure and debug my code more easily. 我想这只是一种习惯,但我认为,它可以帮助我更轻松地构建和调试我的代码。
Couple that with a pathological hatred for IE, which comes natural to anyone doing some client-side scripting. 结合IE的病态仇恨,这对任何做客户端脚本的人来说都很自然。 IE versions that don't support strict mode, tend to leak the function name to the global namespace, thus never allowing the memory associated with the function (and the closure we've created) to be GC'ed. 不支持严格模式的IE版本往往会将函数名称泄漏到全局名称空间,因此永远不会允许与函数关联的内存(以及我们创建的闭包)进行GC操作。 Which might lead to circular references, and, worse still, circular DOM references, which can lead to memory-leaks. 这可能导致循环引用,更糟糕的是,循环DOM引用,这可能导致内存泄漏。

Actually: here's another, real example of where arguments.callee is used: event delegation and detaching event listeners 实际上: 这是另一个使用arguments.callee 实例 :事件委托和分离事件监听器
here's some more info on JS strict mode and recursion using arguments.callee . 这里有一些关于JS严格模式和使用arguments.callee递归的更多信息

The last question has, IMO the most clear cut example of how arguments.callee is handy: recursive replacing functions: 最后一个问题,IMO是关于arguments.callee如何方便的最明确的例子:递归替换函数:

function someF(foo)
{
    //'use strict'; <-- would throw errors here
    foo = foo.replace(/(a|b)+/gi, function (p1,p2)
    {
        if (p1.match(/(a|b){2,}/i))
        {
            return p1.replace(/(a|b)/gi,arguments.callee);//recursive
        }
        return (p2.match(/a/i) ? 'X':'Y');
    });
}

As requested arguments.callee on MDN, warns for usage in strict mode (ECMA 5, that explains why DC says arguments.callee is deprecated) 正如在MDN上请求的arguments.callee ,警告在严格模式下使用(ECMA 5,这解释了为什么DC说arguments.callee已被弃用)
And more on strict 而且更严格

Calee is part of the ECMAScript 3 standard, so it should be safe for cross-browser use. Calee是ECMAScript 3标准的一部分,因此对于跨浏览器使用应该是安全的。 Callee holds the function that is currently executing and invoking it will invoke the current function. Callee保存当前正在执行的函数,调用它将调用当前函数。 Therefore callee takes exactly the same arguments as the enclosing function (or rather it is the current function). 因此callee使用与封闭函数完全相同的参数(或者更确切地说它当前函数)。 Some more information is available here: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Functions_and_function_scope/arguments/callee 有关更多信息,请访问: https//developer.mozilla.org/en-US/docs/JavaScript/Reference/Functions_and_function_scope/arguments/callee

It is considered bad style to use callee. 使用被叫者被认为是不好的风格。 Give you function a name instead and use this name... 为您提供一个名称,并使用此名称...

It specifies the currently executing function, so arguments.callee is the current function. 它指定当前正在执行的函数,因此arguments.callee是当前函数。 It may be helpfull if you need to go recursive in anonimous function. 如果你需要在无穷大的函数中递归,这可能会有所帮助。 Here example from mozilla: 这里来自mozilla的例子:

function create() {
   return function(n) {
      if (n <= 1)
         return 1;
      return n * arguments.callee(n - 1);
   };
}

var result = create()(5); // returns 120 (5 * 4 * 3 * 2 * 1)

callee is a property of the arguments object. callee是arguments对象的属性。 It can be used to refer to the currently executing function inside the function body of that function. 它可用于引用该函数的函数体内当前正在执行的函数。

MDN docs here MDN在这里提供文档

arguments.callee is a round about way of knowing the current executing function by asking 'Who is calling this specific argument?' arguments.callee是一个关于通过询问“谁在调用这个特定参数?”来了解当前执行函数的方法。 . . . .

 function factorial(a){
    if(a>0)
      return a*arguments.callee(a-1);
 }

Here if you call factorial(5), it will check for condition greater than 0, and if it is true, will execute the same logic for the next lesser number . 这里,如果你调用factorial(5),它将检查大于0的条件,如果为真,则将为下一个较小的数字执行相同的逻辑。 . . In some cases you don't know the name of the function to be called . 在某些情况下,您不知道要调用的函数的名称。 . . .so you can use this property .so你可以使用这个属性

here is a good reference 这是一个很好的参考

arguments.callee from MDN 来自MDN的arguments.callee

UPDATE: arguments.callee() is deprecated in ES5 更新:在ES5中不推荐使用arguments.callee()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM