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

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

    If you are wanting to assign a global variable only if it doesn't already exist, try:

    window.someVar = window.someVar || 'hi';
    

    or

    window['someVar'] = window['someVar'] || 'hi';
    
    0 讨论(0)
  • 2020-12-13 06:07

    As of ES6 most of other answers, including the accepted answer, are incorrect, because global variables defined by let or const, or resulting from a class declaration, do not have corresponding properties on the global object (window in a browser, or global in node.js). Several of them—mainly the ones which use typeof—can also be fooled by global variables which exist but which are set to undefined.

    The only fully general way to test to see if a global variable exists—regardless of whether it has been declared using var, let or const, created via a function or class declaration, created by assignment (i.e., myVar = value at the top level of a program without any declaration for myVar) or by creating a property on the global object (i.e., window.myVar = value)—is to attempt to access it via a global eval and see if TypeError is thrown.

    (This builds on an idea presented by Ferran Maylinch, but with a trick to ensure that it will work properly even when encapsulated in a function.)

    function globalExists(varName) {
      // Calling eval by another name causes evalled code to run in a
      // subscope of the global scope, rather than the local scope.
      const globalEval = eval;
      try {
        globalEval(varName);
        return true;
      } catch (e) {
        return false;
      }
    }
    
    undeclared = undefined;
    const myConst = undefined;
    let myLet;
    var myVar;
    
    globalExists('undeclared')    // => true
    globalExists('myConst')       // => true
    globalExists('myLet')         // => true
    globalExists('myVar')         // => true
    globalExists('nonexistent')   // => false
    globalExists('globalExists')  // => true - can see itself.
    globalExists('varName')       // => false - not fooled by own parameters.
    globalExists('globalEval')    // => false - not fooled by local variable.
    

    Note that this makes use of eval, so all the usual caveats apply: you should not supply an untrusted value as the parameter, and if you must use an untrusted value you should check to make sure that varName is a valid JavaScript identifier. Doing so is out of scope for this question, but it can be done using a (rather complex) regular expression—just beware that the correct regexp depends on the version of ECMAScript you are using, whether the code is a script or (ES6) module, whether it is in an async function, etc. etc.

    0 讨论(0)
  • 2020-12-13 06:13

    I think this is actually a problem with JSLint. It will issue the following error:

    Unexpected 'typeof'. Compare directly with 'undefined'.

    I believe this is bad advice. In JavaScript, undefined is a global variable that is, usually, undefined. But some browsers allow scripts to modify it, like this: window.undefined = 'defined'. If this is the case, comparing directly with undefined can lead to unexpected results. Fortunately, current ECMA 5 compliant browsers do not allow assignments to undefined (and will throw an exception in strict mode).

    I prefer typeof someVar === "undefined", as you posted, or someVar in window as Susei suggested.

    0 讨论(0)
  • 2020-12-13 06:15

    I think the best solution is the following:

    if(window.hasOwnProperty('foo')) {
        console.log('Variable is not declared');
    }
    

    The following solution will not work if the variables is declared but is not assigned (var foo;).

    typeof foo === 'undefined'
    
    0 讨论(0)
  • 2020-12-13 06:16

    try

    variableName in window
    

    or

    typeof window[variableName] != 'undefined'
    

    or

    window[variableName] !== undefined
    

    or

    window.hasOwnProperty(variableName)
    
    0 讨论(0)
  • 2020-12-13 06:17
    if (typeof someVar === "undefined") {
        var someVar = "hi!";
    }
    

    will check if someVar (local or global) is undefined.

    If you want to check for a global variable you can use

    if(window['someVar'] === undefined) {
        ...
    }
    

    assuming this is in a browser :)

    0 讨论(0)
提交回复
热议问题