Javascript arguments.callee的作用是什么 [英] Javascript arguments.callee what is it for

查看:110
本文介绍了Javascript arguments.callee的作用是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尚未找到此变量的任何完整的跨浏览器文档.

I haven't found any complete cross-browser doc of this variable.

arguments.callee的作用是什么?如何运作?

What is arguments.callee for? how does it work?

它有哪些论点?

推荐答案

arguments.callee是对当前正在调用的函数的引用.第一件事:不要使用它:如果您处于严格的环境中,它只会喷出错误.

arguments.callee is a reference to the function that is currently being called. 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')));

我希望您喜欢闭包,因为我喜欢-这就是arguments.callee很可能发生的地方.倒数第二行是钱在哪里:

I hope you like closures, because I do - and that's where arguments.callee are very likely to occur. The next-to-last line is where the money is:

(arguments.callee)

是对匿名函数的引用,该匿名函数在闭包范围内(在这种情况下,可以访问1个DOM元素)设置初始超时.匿名函数返回后会进行GC处理,但是在这种情况下,我将其添加到超时回调的作用域中(将其作为参数返回给另一个返回实际回调的匿名函数),因此仍在某处引用它.
现在,如果您处于严格的状态,则不必担心,因为这是严格模式下的代码:

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). 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.
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')));

命名函数,仅此而已.我为什么不喜欢它? arguments.callee引发标志,就像进行某些关闭欺骗的匿名函数一样.我想这只是一种习惯,但是我觉得它是一种习惯,可以帮助我更轻松地构建和调试代码.
将其与对IE的病理性仇恨相结合,这对进行某些客户端脚本编写的任何人来说都是很自然的.不支持严格模式的IE版本倾向于泄漏函数名称到全局名称空间,因此永远不允许与该函数关联的内存(以及我们创建的闭包)为GC' ed.这可能导致循环引用,更糟糕的是,导致循环DOM引用,这可能导致内存泄漏.

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. 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 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. Which might lead to circular references, and, worse still, circular DOM references, which can lead to memory-leaks.

实际上:这是另一个真实的例子arguments.callee的位置的a>:事件委托和分离事件侦听器
这是有关JS严格模式和使用arguments.callee进行递归的更多信息.

Actually: here's another, real example of where arguments.callee is used: event delegation and detaching event listeners
here's some more info on JS strict mode and recursion using arguments.callee.

最后一个问题是,IMO是arguments.callee方便使用的最清晰的例子:递归替换函数:

The last question has, IMO the most clear cut example of how arguments.callee is handy: recursive replacing functions:

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');
    });
}

根据要求 arguments.callee MDN警告在严格模式下使用(ECMA 5,这解释了为什么DC不推荐使用arguments.callee)

As requested arguments.callee on MDN, warns for usage in strict mode (ECMA 5, that explains why DC says arguments.callee is deprecated)
And more on strict

这篇关于Javascript arguments.callee的作用是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆