JS 變數和函式提升 全域性變數和區域性變數
阿新 • • 發佈:2018-11-07
1.
var a = 10; function test() { a = 100; console.log(a); console.log(this.a); var a; console.log(a); } test();//100 10 100
=>
var a = 10;//全域性 function test() { var a;//變數提升 a = 100;//區域性 console.log(a);//區域性 console.log(this.a);//this指向全域性window console.log(a);//區域性 } test();//100 10 100
2.
var a = 100; function test() { console.log(a); var a = 10; console.log(a); } test();//undefined 10
=>
var a = 100; function test() { var a;//變數提升 console.log(a); a = 10; console.log(a); } test();//undefined 10
3.
var a = 100; function test() { console.log(a); a= 10; console.log(a); } test(); console.log(a); //100 10 10
=>
var a = 100; function test() { console.log(a);//全域性 a = 10;//全域性 console.log(a);//全域性 } test(); console.log(a); //100 10 10