检查全局变量是否存在的正确方法是什么?
问题描述:
JSLint未将此作为有效代码传递:
JSLint is not passing this as a valid code:
/* global someVar: false */
if (typeof someVar === "undefined") {
var someVar = "hi!";
}
正确的方法是什么?
What is the correct way?
答
/*global window */
if (window.someVar === undefined) {
window.someVar = 123456;
}
if (!window.hasOwnProperty('someVar')) {
window.someVar = 123456;
}