Javascript arguments.callee 是干什么用的 [英] Javascript arguments.callee what is it for
问题描述
我还没有找到这个变量的任何完整的跨浏览器文档.
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'编.这可能会导致循环引用,更糟糕的是,循环 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
的地方:事件委托和分离事件侦听器
这里有更多关于 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.calleeMDN,警告在严格模式下使用(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屋!