js-关于this的小测试

一个面试题分析:

<script type="text/javascript">
    //运行test() 和new test()的结果
    var a = 5;
    function test(){
        a = 0;
        alert(a);
        alert(this.a);
        var a;
        alert(a);
    }
    test(); //0 5 0
    // AO{
    //  a: 0
    // }
    // GO {
    //  a: 5
    // }
    // alert(this.a);相当于window里面即Go里面的a
     new test();//0 undefined 0
     // AO{
     //     this:{
     //         _proto_: test.prototype
     //     },//new时会在AO里面会出现一个this属性
     //     a: 0
     // }
     // alert(this.a);由AO可见this里面没有属性a

</script>

猜你喜欢

转载自blog.csdn.net/qq_39396275/article/details/81698997