[英]How do I check if the Javascript global object has been tampered with?
I just had this really dumb bug that took 20m to find simply because I forgot the var
keyword somewhere. 我只是遇到了一个非常愚蠢的bug,花了20m的时间才找到它,只是因为我忘记了某个地方的
var
关键字。 I figured it out, but it just got me thinking... 我想通了,但这只是让我思考...
"Is there an easy way to see if any properties were added to the global object?" “有没有一种简单的方法来查看是否将任何属性添加到全局对象?”
Edit: Sorry, forgot to mention the context is in Node.js. 编辑:对不起,忘了提到上下文在Node.js中。
ES5's strict mode prevents this! ES5的严格模式可以防止这种情况! Just put a
'use strict'
statement at the top of any function or source file: 只需在任何函数或源文件的顶部放置
'use strict'
语句:
'use strict';
function foo() {
var x = 5 // Forgot the comma!
y = 6; // Fails in strict mode as an implicit global
}
foo();
Linters should also check for this, and you should use both a linter and strict mode, ideally. 短毛猫还应该检查这一点,理想情况下,您应该同时使用短毛猫和严格模式。 I recommend JSHint .
我推荐JSHint 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.