关于javascript的this指向的一些探索

本文深入解析JavaScript中this关键字的不同应用场景,包括全局作用域、构造函数、原型链继承等情况下的指向规则,并通过具体示例帮助理解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.this指向全局对象

function test(){
                this.x=3;
            }
            test();
            console.log(x)  //打印出3

2.this指向构造函数new出来的实例内部

var x=8;
            function test(){
                   this.x=90;
                 console.log(this.x)
            }
            new test();  //打印出90,

 

3.this指向上一级对象

var o={};
            o.x=990;
            var x=888;
            o.test=function(){
                console.log(this.x)
            }
            o.test()  //打印出990

4.this可以通过apply方法改变this的指向

           var x=666;
            var o={};
            o.x=1;
            o.test=function(){
                console.log(this.x);
            }
            o.test();//打印出1,指向了上一级
            o.test.apply();//打印出666,指向了全局对象
            o.test.apply(o);//打印出1,指向上一级

5.关于构造函数的this指向问题,原则就近原则,没有就沿着原型链往上找

①     function Test(){
                this.a=66;
                  this.b=55;
                this.show=function(){
                    console.log(this.a,this.b);
                }
            }
            function Big(){
                this.a=125;
                this.b=2;
                this.add=function(){
                    console.log(this.a,this.b);
                }
            }

           //通过这个实现构造函数Test继承Big
            Test.prototype=new Big();
            var x=new Test();
            x.show();//打印出66,55

②     function Test(){
                this.show=function(){
                    console.log(this.a,this.b);
                }
            }
            function Big(){
                this.a=125;
                this.b=2;
                this.add=function(){
                    console.log(this.a,this.b);
                }
            }

           //通过这个实现构造函数Test继承Big
            Test.prototype=new Big();
            var x=new Test();
            x.show();//打印出125,2

③  function Test(){

                this.a=6587;
                this.show=function(){
                    console.log(this.a,this.b);
                }
            }
            function Big(){
                this.a=125;
                this.b=2;
                this.add=function(){
                    console.log(this.a,this.b);
                }
            }

           //通过这个实现构造函数Test继承Big
            Test.prototype=new Big();
            var x=new Test();
            x.show();//打印出6587,2

④ function Test(){
                this.a=6587;
                this.show=function(){
                    console.log(this.a,this.b);
                }
            }
            function Big(){
                this.a=125;
                this.b=2;
                this.add=function(){
                    console.log(this.a,this.b);
                }
            }
            Test.prototype=new Big();
            var x=new Test();
            x.add();//打印出6587,2

⑤function Test(){
                this.a=6587;
                this.show=function(){
                    console.log(this.a,this.b);
                }
            }
            function Big(){
                this.a=125;
                this.b=2;
                this.add=function(){
                    console.log(this.a,this.b);
                }
            }
            Test.prototype=new Big();
            var x=new Test();

           //在这里对实例重新赋值的情况

            x .a=54321
            x.add();//打印出54321,2

 

博主经验总结得出下图:

就近原则,如果变量this指向在构造函数内部不存在就沿着原型连往上找,如果同时存在就会优先考虑所new出来对象的构造函数内部的

以上就是博主的一些总结,小伙伴们多练多钻研多总结吧

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值