简体   繁体   English

如何检查Javascript全局对象是否已被篡改?

[英]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.

相关问题 如何检查 object 在 JavaScript 中是否具有特定属性? - How do I check if an object has a specific property in JavaScript? 如何检查任何地方是否触发了Javascript事件? - How do I check if any Javascript event has been fired, anywhere? JavaScript - 检查 Object URL 是否已被使用 - JavaScript - Check if Object URL has been used 检查 JavaScript 对象是否已经被实例化 - Check if a JavaScript object has already been instantiated 检查会话是否已启动,并设置全局变量以供JavaScript使用 - Check if session has been started and set global var for javascript to use 检查是否已在JavaScript中覆盖全局属性/函数 - Check if a global property/function has been overwritten in JavaScript 如何检查是否已设置JavaScript-Eventhandler? - How can i check if a JavaScript-Eventhandler has been set? 检查是否已单击书签而不在 JavaScript 中设置全局变量 - Check if bookmark has been clicked without setting a global variable in JavaScript 如何检查特定模块是否已添加到 Worklet? - How do I check if a specific module has been added to a Worklet? 如果requireJS模块在$上设置了全局变量,那么仅在分配了全局变量后,如何执行模块代码? - if a requireJS module sets a global on $, how do I execute the module code only when the global has been assigned?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM