so_brave 发表于 2011-12-08 20:33

javascript函数中变量的范围

javascript函数中变量的范围











Java代码
var first = 'hi there';   
var first = (function() {   
    console.log("first", first); // undefined   
    var first = "hello world";   
})();   
//相当于   
var first = 'hi there';   
var first = (function() {   
    var first;//the variable declaration moved to the top   
    console.log("first", first); // undefined   
    var first = "hello world";   
})();   

// second test case   
var second = 'hi there';   
var second = (function() {   
    console.log("second", second ); // "hi there"   
    // here, we DO NOT declare a local "second" variable   
})();   

// in the second test case, we don't declare the local variable, so the console.log call refers to the global variable and the value is not "undefined"   
//link:http://jsfiddle.net/pomeh/ynqBs/

在我心中舞动 发表于 2011-12-24 20:38

谢谢分享希望于楼主多多交流
页: [1]
查看完整版本: javascript函数中变量的范围