是否有充分的理由将字符串传递给 setTimeout?
我们都知道,将字符串传递给 setTimeout
(或 setInterval
) 是邪恶的,因为它在全局范围内运行,存在性能问题,如果您注入任何参数等。所以这样做绝对是不推荐的:
setTimeout('doSomething(someVar)', 10000);
赞成这样做:
setTimeout(function() {
doSomething(someVar);
}, 10000);
我的问题是:有理由这样做前者吗?它曾经更可取吗?如果不是,为什么会被允许?
我想到的唯一场景是想要使用全局作用域中存在但已在本地作用域中被覆盖的函数或变量。然而,在我看来,这听起来像是糟糕的代码设计......
We all know that passing a string to setTimeout
(or setInterval
) is evil, because it is run in the global scope, has performance issues, is potentially insecure if you're injecting any parameters, etc. So doing this is definitely deprecated:
setTimeout('doSomething(someVar)', 10000);
in favour of this:
setTimeout(function() {
doSomething(someVar);
}, 10000);
My question is: can there ever be a reason to do the former? Is it ever preferable? If it isn't, why is it even allowed?
The only scenario I've thought of is of wanting to use a function or variable that exists in the global scope but has been overridden in the local scope. That sounds to me like poor code design, however...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您始终可以通过将全局变量作为 window 对象的属性访问来使用它们,例如 window.globalVar (尽管使用全局变量确实不是一个好的做法),所以不,我认为从来没有这是使用已弃用语法的充分理由。
这可能是由于历史原因而被允许的:正如 Felix Kling 提到的,原始语法只允许传递一串代码:
如果浏览器不支持使用字符串不再作为
setTimeout
和setInterval
的第一个参数,互联网上会有很多代码不再起作用。You can always use global variables by accessing them as properties of the window object, like
window.globalVar
(though using globals is indeed not a good practice), so no, I don't think there is ever a good reason to use the deprecated syntax.It is probably allowed for historical reasons: as Felix Kling mentioned, the original syntax did only allow to pass a string of code:
If browsers don't support the use of a string as first argument to
setTimeout
andsetInterval
anymore, there will be lots of code on the internet that doesn't function anymore.对于那些因为什么传递函数比传递字符串更好问题而重定向到这里的人。
1:传递字符串会启动编译器
每次必须计算字符串时,都会启动完整的编译器。对于每次需要的调用。
这不仅很慢,而且还破坏了所有已完成的 JIT 和浏览器加速。
2:传递字符串受到更多限制。
因为字符串是通过编译器运行的,所以它不能清晰地绑定到本地范围和变量。
虽然在以下情况下并不明显:
在更复杂的情况下,代码更简洁:
vs
3: DOM 对象不能通过字符串传递
正如 Álvaro 提到的,DOM 对象不能通过通过字符串方法传递。
(其他对象可能会也可能不会通过——取决于它们是否可以序列化,但总的来说这是相当困难的。)
For those who are redirected here by the question of why is passing a function better than passing a string.
1: Passing a string fires up a compiler
Every time you have to evaluate a string, you fire up a full compiler. For each and every invocation where it is needed.
Not only is this slow, it destroys all of the JIT and browser speedups that are done.
2: Passing a string is MUCH more limited.
Because a string is run through a compiler, it isn't as cleanly bound to the local scope and variables.
While it isn't noticeable in situation like:
In a more complex situation, the code is just cleaner:
vs
3: DOM objects can't be passed via string
As Álvaro mentioned, DOM objects can not be passed via a string method.
(Other objects may or may not be passable -- depending on if they can be serialized, but but in general it would be quite difficult.)