Detecting an undefined object property

后端 未结 30 2838
花落未央
花落未央 2020-11-21 04:43

What\'s the best way of checking if an object property in JavaScript is undefined?

30条回答
  •  [愿得一人]
    2020-11-21 05:32

    Crossposting my answer from related question How can I check for "undefined" in JavaScript?.

    Specific to this question, see test cases with someObject..


    Some scenarios illustrating the results of the various answers: http://jsfiddle.net/drzaus/UVjM4/

    (Note that the use of var for in tests make a difference when in a scoped wrapper)

    Code for reference:

    (function(undefined) {
        var definedButNotInitialized;
        definedAndInitialized = 3;
        someObject = {
            firstProp: "1"
            , secondProp: false
            // , undefinedProp not defined
        }
        // var notDefined;
    
        var tests = [
            'definedButNotInitialized in window',
            'definedAndInitialized in window',
            'someObject.firstProp in window',
            'someObject.secondProp in window',
            'someObject.undefinedProp in window',
            'notDefined in window',
    
            '"definedButNotInitialized" in window',
            '"definedAndInitialized" in window',
            '"someObject.firstProp" in window',
            '"someObject.secondProp" in window',
            '"someObject.undefinedProp" in window',
            '"notDefined" in window',
    
            'typeof definedButNotInitialized == "undefined"',
            'typeof definedButNotInitialized === typeof undefined',
            'definedButNotInitialized === undefined',
            '! definedButNotInitialized',
            '!! definedButNotInitialized',
    
            'typeof definedAndInitialized == "undefined"',
            'typeof definedAndInitialized === typeof undefined',
            'definedAndInitialized === undefined',
            '! definedAndInitialized',
            '!! definedAndInitialized',
    
            'typeof someObject.firstProp == "undefined"',
            'typeof someObject.firstProp === typeof undefined',
            'someObject.firstProp === undefined',
            '! someObject.firstProp',
            '!! someObject.firstProp',
    
            'typeof someObject.secondProp == "undefined"',
            'typeof someObject.secondProp === typeof undefined',
            'someObject.secondProp === undefined',
            '! someObject.secondProp',
            '!! someObject.secondProp',
    
            'typeof someObject.undefinedProp == "undefined"',
            'typeof someObject.undefinedProp === typeof undefined',
            'someObject.undefinedProp === undefined',
            '! someObject.undefinedProp',
            '!! someObject.undefinedProp',
    
            'typeof notDefined == "undefined"',
            'typeof notDefined === typeof undefined',
            'notDefined === undefined',
            '! notDefined',
            '!! notDefined'
        ];
    
        var output = document.getElementById('results');
        var result = '';
        for(var t in tests) {
            if( !tests.hasOwnProperty(t) ) continue; // bleh
    
            try {
                result = eval(tests[t]);
            } catch(ex) {
                result = 'Exception--' + ex;
            }
            console.log(tests[t], result);
            output.innerHTML += "\n" + tests[t] + ": " + result;
        }
    })();
    

    And results:

    definedButNotInitialized in window: true
    definedAndInitialized in window: false
    someObject.firstProp in window: false
    someObject.secondProp in window: false
    someObject.undefinedProp in window: true
    notDefined in window: Exception--ReferenceError: notDefined is not defined
    "definedButNotInitialized" in window: false
    "definedAndInitialized" in window: true
    "someObject.firstProp" in window: false
    "someObject.secondProp" in window: false
    "someObject.undefinedProp" in window: false
    "notDefined" in window: false
    typeof definedButNotInitialized == "undefined": true
    typeof definedButNotInitialized === typeof undefined: true
    definedButNotInitialized === undefined: true
    ! definedButNotInitialized: true
    !! definedButNotInitialized: false
    typeof definedAndInitialized == "undefined": false
    typeof definedAndInitialized === typeof undefined: false
    definedAndInitialized === undefined: false
    ! definedAndInitialized: false
    !! definedAndInitialized: true
    typeof someObject.firstProp == "undefined": false
    typeof someObject.firstProp === typeof undefined: false
    someObject.firstProp === undefined: false
    ! someObject.firstProp: false
    !! someObject.firstProp: true
    typeof someObject.secondProp == "undefined": false
    typeof someObject.secondProp === typeof undefined: false
    someObject.secondProp === undefined: false
    ! someObject.secondProp: true
    !! someObject.secondProp: false
    typeof someObject.undefinedProp == "undefined": true
    typeof someObject.undefinedProp === typeof undefined: true
    someObject.undefinedProp === undefined: true
    ! someObject.undefinedProp: true
    !! someObject.undefinedProp: false
    typeof notDefined == "undefined": true
    typeof notDefined === typeof undefined: true
    notDefined === undefined: Exception--ReferenceError: notDefined is not defined
    ! notDefined: Exception--ReferenceError: notDefined is not defined
    !! notDefined: Exception--ReferenceError: notDefined is not defined
    

提交回复
热议问题