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

后端 未结 10 1393
死守一世寂寞
死守一世寂寞 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:16

    try

    variableName in window
    

    or

    typeof window[variableName] != 'undefined'
    

    or

    window[variableName] !== undefined
    

    or

    window.hasOwnProperty(variableName)
    

提交回复
热议问题