【发布时间】:2012-01-11 19:06:22
【问题描述】:
为了做类似的事情
setTimeout(function () {
...
setTimeout(arguments.callee, 100);
}, 100);
我需要arguments.callee 之类的东西。我发现information at javascript.info arguments.callee 已被弃用:
此属性已被 ECMA-262 弃用,取而代之的是命名函数 表达式和更好的性能。
但是应该用什么代替呢?像这样的东西?
setTimeout(function myhandler() {
...
setTimeout(myhandler, 100);
}, 100);
// has a big advantage that myhandler cannot be seen here!!!
// so it doesn't spoil namespace
顺便说一句,arguments.callee 是否兼容跨浏览器?
【问题讨论】:
-
是的,你应该为你的函数命名并在 setTimeout 中使用它的名字。
-
@cHao,不,这不是重复的:arguments.callee.caller deprecation 并不意味着整个 arguments.callee 的弃用。而且,这个问题没有问“应该用什么代替!”
-
@Tomas:检查答案。你问的每件事都会在里面提到。