我的一个朋友正在进行在线测验,他问我这个我无法回答的问题.
- var global = false;
- function test() {
- global = true;
- return false;
- function global() {}
- }
- console.log(global); // says false (As expected)
- test();
- console.log(global); // says false (Unexpected: should be true)
如果我们假设函数在顶部和var变量一起被提升,让我们试试这个.
- var foo = 1;
- function bar() {
- return foo;
- foo = 10;
- function foo() {}
- var foo = 11;
- }
- bar();
- console.log(foo); //says 1 (But should be 11) Why 1 this time ??
这是一个JSBin Demo和JSBIN Demo2玩.