js 返回 undefined 值的情况

  • 来源 [三生石上] 翻译的JavaScript 秘密花园 (http://bonsaiden.github.io/JavaScript-Garden/zh/#core.undefined)

  • 访问声明,但是没有初始化的变量

    var aaa;
    console.log(aaa); // undefined
    
  • 访问不存在的属性

    var aaa = {};
    console.log(aaa.c);
    
  • 访问函数的参数没有被显式的传递值

    (function (b){
        console.log(b); // undefined
    })();
    
  • 访问任何被设置为 undefined 值的变量

    var aaa = undefined;
    console.log(aaa); // undefined
    
  • 没有定义 return 的函数隐式返回

    function aaa(){}
    console.log(aaa()); // undefined
    
  • 函数 return 没有显式的返回任何内容

    function aaa(){
        return;
    }
    console.log(aaa()); // undefined
    
posted @ 2014-02-13 15:41  责约果  阅读(2487)  评论(0编辑  收藏  举报