求解?这里为什么foo的值为undefined呢???请各位大神赐教。。。

代码:
var foo = "test";
function test(){
console.log("foo:"+foo);
var foo = "new test";
}
test();
结果:
foo:undefined

求解?这里为什么foo的值为undefined呢???请各位大神赐教。。。

这个在javascript中叫“声明提前”,在js中没有块级作用域,
[code="java"]
function test(){
console.log("foo:"+foo);
var foo = "new test";
} [/code]
相当于
[code="java"]
function test(){
var foo;
console.log("foo:"+foo);
foo = "new test";
} [/code]

推荐链接:http://www.cnblogs.com/TomXu/archive/2011/12/29/2290308.html

建议你看看http://net.tutsplus.com/tutorials/javascript-ajax/quick-tip-javascript-hoisting-explained/这里面讲得很清楚。