你不知道的js坑

271 阅读1分钟
1.作用域链查找规则:从函数定义开始时候确定。

var hello = '123';

((function(){
    var hello = 'world';
    console.log(hello);
    test();
})());

function test() {
    console.log(hello);
}

结果: world 123

2.变量声明提升,而不是变量定义提升。

function test() {
    var x = 1;
    console.log(x, y);
    var y = 2;
}

test();

结果: 1, undefined