Code01:

<script>
var a = 10;
function fn(){
    a = 100;//底下的var a提升声明,所以这里的a是局部变量a
    console.log(a);
    console.log(this.a);//fn()在全局作用域中执行,所以this指向全局。
    var a;
    console.log(a);//局部变量a
}
fn();
</script>

结果:100,10,100

Code02:

<script>
var a=10;
function fn(){
    console.log(a);//底下的var a=100;会提升声明,但不会赋值,所以是undefined
    console.log(this.a);//全局变量a
    var a = 100;
    console.log(a);//局部变量a
}
fn();
</script>
结果:undefined,10,100

Code03:

<script>
var a=10;
function fn(){
    console.log(a);//-->全局遍历a
    console.log(this.a);//-->全局遍历a
    a = 100;
}
fn();
console.log(a);//-->全局遍历a
</script>

结果:10,10,100


Asrocky
420 声望36 粉丝