美文网首页让前端飞Web前端之路II 极客公园 II
解决这两种this混乱的场景,让它的指向明明白白!

解决这两种this混乱的场景,让它的指向明明白白!

作者: 视觉派Pie | 来源:发表于2019-09-27 07:37 被阅读0次

    this永远指向当前函数的主人,在构造函数中this指向新创建的对象,说明构造函数的主人是新创建的对象。但是有以下两种场景会引起this指向的混乱:

    加了定时器

    new去创建对象的时候,this指向新创建的对象也就是a1,当我们调用a1.show()的时候输出10。

            function Aaa(){
                this.a = 10;
            }
            Aaa.prototype.show = function(){
                alert(this.a);
            }
            var a1 = new Aaa();
            a1.show();//10
    

    但是当我们在构造函数中添加一个定时器,让它4秒后调用this.show,输出undefined。

            function Aaa(){
                this.a = 10;
                setInterval(this.show,4000);
            }
    
            Aaa.prototype.show = function(){
                alert(this.a);
            }
            var a1 = new Aaa();
            a1.show();
    

    我们在原型方法上打印this,发现4秒之后,this指向的是当前窗口[object Window],this.show本来是a1的方法,但是把this.show这个函数当参数,传给定时器后this指向了window,函数的主人变成了window。

            function Aaa(){
                this.a = 10;
                setInterval(this.show,4000);
            }
    
            Aaa.prototype.show = function(){
                alert(this);
            }
            var a1 = new Aaa();
            a1.show();
    

    为了避免这种情况,我们在给定时器传参之前,将this赋值给一个新的变量_this,将this存储一下。现在我们再执行a1.show()这个方法,4秒之后打印的还是10,这样就不会引起指向混乱的问题。

        function Aaa(){
            this.a = 10;
            //起一个变量,将this存储一下
            var _this = this;
            /*
                把this.show这个函数,当参数传给定时器。
            */
            setInterval(function(){
                _this.show();
            },4000);
        }
        Aaa.prototype.show = function(){
            alert(this.a);//10
        }
    
        var a1 = new Aaa();
        a1.show();//10
    

    事件绑定

    现在创建一个Bbb的构造函数,通过new生成一个b1对象,调用b1.show()方法输出20。

            window.onload = function(){
                function Bbb(){
                    this.b = 20;
                }
    
                Bbb.prototype.show = function(){
                    alert(this.b);
                }
    
                var b1 = new Bbb();
                b1.show();//20
    

    在页面中添加一个button,把方法放在window.onload中,获取按钮,给按钮添加点击事件,将this.show绑定在按钮身上。当点击按钮的时候,执行this.show函数,输出值为undefined。

    <head>
        <meta charset="UTF-8">
        <title>Document</title>
        <script>
            window.onload = function(){
                function Bbb(){
                    this.b = 20;
                    var oBtn = document.getElementById("btn1");
                    oBtn.onclick = this.show();//undefined
                }
    
                Bbb.prototype.show = function(){
                    alert(this.b);
                }
                var b1 = new Bbb();
                b1.show();//20
            }
        </script>
    </head>
    <body>
        <button id = "btn1">按钮</button>
    </body>
    

    我们还是在原型方法上输出this,页面加载完成的时候this指向当前对象[object Object],但是点击按钮之后this指向Button[object HTMLButtonElement]

                Bbb.prototype.show = function(){
                    alert(this);
                }
    

    添加定时器是将this.show作为参数传递,事件绑定是将this.show作为值赋给Button,现在我们还是将this存储在_this中,然后给Button绑定事件的时候将方法写在function中,这样this指向的还是原来的对象。

            window.onload = function(){
                function Bbb(){
                    this.b = 20;
                    var _this = this;
                    var oBtn = document.getElementById("btn1");
                    /*
                        将this.show 赋值给按钮的点击
                    */
                    oBtn.onclick = function(){
                        _this.show()
                    }
                }
    
                Bbb.prototype.show = function(){
                    alert(this.b);
                }
    
                var b1 = new Bbb();
                b1.show();//20
    

    相关文章

      网友评论

        本文标题:解决这两种this混乱的场景,让它的指向明明白白!

        本文链接:https://www.haomeiwen.com/subject/pyxoectx.html