What is the correct way to check if a global variable exists?

后端 未结 10 1358
死守一世寂寞
死守一世寂寞 2020-12-13 06:02

JSLint is not passing this as a valid code:

/* global someVar: false */
if (typeof someVar === \"undefined\") {
    var someVar = \"hi!\";
}
<
10条回答
  •  时光说笑
    2020-12-13 06:18

    /*global window */
    
    if (window.someVar === undefined) {
        window.someVar = 123456;
    }
    
    if (!window.hasOwnProperty('someVar')) {
        window.someVar = 123456;
    }
    

提交回复
热议问题