Javascript arguments.callee 是干什么用的
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12050183/
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
Javascript arguments.callee what is it for
提问by Jordi P.S.
I haven't found any complete cross-browser doc of this variable.
我还没有找到这个变量的任何完整的跨浏览器文档。
What is arguments.callee
for? how does it work?
是arguments.callee
为了什么?它是如何工作的?
Which arguments does it have?
它有哪些论据?
回答by Elias Van Ootegem
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.
arguments.callee
是对当前正在调用的函数的引用。首先要做的是:不要使用它:如果您处于严格的上下文中,它只会产生错误。
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. The next-to-last line is where the money is:
我希望你喜欢闭包,因为我喜欢——而且这arguments.callee
很可能发生。倒数第二行是钱在哪里:
(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). 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:
是对在闭包范围内设置初始超时的匿名函数的引用(在这种情况下可以访问 1 个 DOM 元素)。匿名函数在返回后会被 GC 处理,但在这种情况下,我已将其添加到超时回调的作用域(将其作为参数传递给另一个返回实际回调的匿名函数),因此它仍然在某处被引用。
现在,如果你是严格的,你不必担心,因为这就是严格模式下的代码:
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. 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 leakthe 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
引发标志,就像一些闭包诡计正在进行的匿名函数一样。我想这只是一种习惯,但我觉得它可以帮助我更轻松地构建和调试代码。
再加上对 IE 的病态仇恨,这对任何编写客户端脚本的人来说都是很自然的。不支持严格模式的 IE 版本倾向于将函数名称泄漏到全局命名空间,因此永远不允许与函数(以及我们创建的闭包)关联的内存被 GC 处理。这可能会导致循环引用,更糟糕的是,循环 DOM 引用会导致内存泄漏。
Actually: here's another, real exampleof where arguments.callee
is used: event delegation and detaching event listeners
here's some more infoon JS strict mode and recursion using arguments.callee
.
实际上:这是另一个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.calleeon MDN, warns for usage in strict mode (ECMA 5, that explains why DC says arguments.callee is deprecated)
And more on strict
作为MDN 上要求的arguments.callee,警告在严格模式下使用(ECMA 5,这解释了为什么 DC 说 arguments.callee 已被弃用)
以及更多关于严格
回答by Mathias Schwarz
Calee is part of the ECMAScript 3 standard, so it should be safe for cross-browser use. Callee holds the function that is currently executing and invoking it will invoke the current function. Therefore callee takes exactly the same arguments as the enclosing function (or rather it isthe current function). Some more information is available here: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Functions_and_function_scope/arguments/callee
Calee 是 ECMAScript 3 标准的一部分,因此跨浏览器使用应该是安全的。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...
使用 callee 被认为是不好的风格。给你一个函数名称,然后使用这个名称......
回答by Danil Speransky
It specifies the currently executing function, so arguments.callee
is the current function. It may be helpfull if you need to go recursive in anonimous function. Here example from mozilla:
它指定当前正在执行的函数,arguments.callee
当前函数也是如此。如果您需要在匿名函数中递归,这可能会有所帮助。这是来自 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)
回答by palerdot
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 . . . In some cases you don't know the name of the function to be called . . . .so you can use this property
在这里,如果您调用 factorial(5),它将检查大于 0 的条件,如果为真,将对下一个较小的数字执行相同的逻辑。. . 在某些情况下,您不知道要调用的函数的名称。. . .所以你可以使用这个属性
here is a good reference
这是一个很好的参考
UPDATE:arguments.callee()
is deprecated in ES5
更新:arguments.callee()
在 ES5 中已弃用
回答by benqus
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.
callee 是 arguments 对象的一个属性。它可用于在该函数的函数体内引用当前正在执行的函数。