JavaScript: undefined !== undefined?

NOTE: As per ECMAScript5.1, section 15.1.1.3, window.undefined is read-only.

  • Modern browsers implement this correctly. for example: Safari 5.1, Firefox 7, Chrome 20, etc.
  • Undefined is still changeable in: Chrome 14, ...
  • When I recently integrated Facebook Connect with Tersus, I initially received the error messages Invalid Enumeration Value and Handler already exists when trying to call Facebook API functions.

    It turned out that the cause of the problem was

    object.x === undefined
    

    returning false when there is no property 'x' in 'object'.

    I worked around the problem by replacing strict equality with regular equality in two Facebook functions:

    FB.Sys.isUndefined = function(o) { return o == undefined;};
    FB.Sys.containsKey = function(d, key) { return d[key] != undefined;};
    

    This made things work for me, but seems to hint at some sort of collision between Facebook's JavaScript code and my own.

    What could cause this?

    Hint: It is well documented that undefined == null while undefined !== null . This is not the issue here. The question is how comes we get undefined !== undefined .


    The problem is that undefined compared to null using == gives true. The common check for undefined is therefore done like this:

    typeof x == "undefined"
    

    this ensures the type of the variable is really undefined.


    It turns out that you can set window.undefined to whatever you want, and so get object.x !== undefined when object.x is the real undefined. In my case I inadvertently set undefined to null.

    The easiest way to see this happen is:

    window.undefined = null;
    alert(window.xyzw === undefined); // shows false
    

    Of course, this is not likely to happen. In my case the bug was a little more subtle, and was equivalent to the following scenario.

    var n = window.someName; // someName expected to be set but is actually undefined
    window[n]=null; // I thought I was clearing the old value but was actually changing window.undefined to null
    alert(window.xyzw === undefined); // shows false
    

    I'd like to post some important information about undefined , which beginners might not know.

    Look at the following code:

     /* 
      * Consider there is no code above. 
      * The browser runs these lines only.
      */
    
       // var a;  
       // --- commented out to point that we've forgotten to declare `a` variable 
    
       if ( a === undefined ) {
           alert('Not defined');
       } else {
           alert('Defined: ' + a);
       }
    
       alert('Doing important job below');
    

    If you run this code, where variable a HAS NEVER BEEN DECLARED using var , you will get an ERROR EXCEPTION and surprisingly see no alerts at all.

    Instead of 'Doing important job below', your script will TERMINATE UNEXPECTEDLY, throwing unhandled exception on the very first line.


    Here is the only bulletproof way to check for undefined using typeof keyword, which was designed just for such purpose:

       /* 
        * Correct and safe way of checking for `undefined`: 
        */
    
       if ( typeof a === 'undefined' ) {
           alert(
               'The variable is not declared in this scope, n' +
               'or you are pointing to unexisting property, n' +
               'or no value has been set yet to the variable, n' + 
               'or the value set was `undefined`. n' +
               '(two last cases are equivalent, don't worry if it blows out your mind.'
               );
       }
    
       /* 
        *  Use `typeof` for checking things like that
        */
    

    This method works in all possible cases.

    The last argument to use it is that undefined can be potentially overwritten in earlier versions of Javascript:

         /* @ Trollface @ */
            undefined = 2;
         /* Happy debuging! */  
    

    Hope I was clear enough.

    链接地址: http://www.djcxy.com/p/19422.html

    上一篇: “this”关键字如何工作?

    下一篇: JavaScript:undefined!== undefined?