JavaScript-自测题15

1
2
3
4
5
6
7
8
9
10
{
var a = 1;
const b = 2;
function test(){}
test = 3;
console.log(typeof test); // number
}
console.log(a); // 1
console.log(typeof test); // function
console.log(b); // b is not defined

执行结果:

1
2
3
4
number
1
function
Uncaught ReferenceError: b is not defined

解析:

  1. test = 3 console.log(type test)为:number
  2. var a 不存在块级作用域限制,所以 console.log(a)为:1
  3. test() 函数声明被提升,所以外部console.log(typeof test)为:function
  4. const b 存在块级作用域限制,所以 b is not defined