zoukankan      html  css  js  c++  java
  • Javascript中的this,bind和that

    Javascript中必须通过this来访问类成员,可是this的特点就是函数绑在哪个对象上,它就指向那个对象。这个可能困扰过很多的程序员,特别是从C#,Java等语言过来的程序员。

    function Foo(){
        this.message = 'This is message from Foo';
    }
    
    Foo.prototype.printMessage = function(){
        console.log(this.message);
    }
    
    function Foo2(){
        this.message = 'This is message from Foo2';
    }
    
    var foo = new Foo();
    foo.printMessage();
    
    var foo2 = new Foo2();
    foo2.printMessage = foo.printMessage;
    foo2.printMessage();

    输出为:

    This is message from Foo

    This is message from Foo2

    主要原因就是this改变了,因此Javascript中this的用法,和C++\C#中的大为不同。如果需要传统方式使用this的函数,可以使用Function.prototype.bind(),指定函数的this值:

    function Foo(){
        this.message = 'This is message from Foo';
        
        this.printMessage = (function(){
            console.log(this.message);
        }).bind(this);
    }
    
    function Foo2(){
        this.message = 'This is message from Foo2';
    }
    
    var foo = new Foo();
    foo.printMessage();
    
    var foo2 = new Foo2();
    foo2.printMessage = foo.printMessage;
    foo2.printMessage();

    输出为:

    This is message from Foo

    This is message from Foo

    另外使用call和apply也可以改变函数调用时的this值。

    bind函数的主要问题是IE9以后才开始提供。并且一旦开始习惯了Javascript的this用法,这种bind反而会不习惯。在实践中,更多用到的还是保存this:

    function Foo(){
        var that = this;
        
        this.message = 'This is message from Foo';
        
        this.printMessage = function(){
            console.log(that.message);
        };
    }
    
    function Foo2(){
        this.message = 'This is message from Foo2';
    }
    
    var foo = new Foo();
    foo.printMessage();
    
    var foo2 = new Foo2();
    foo2.printMessage = foo.printMessage;
    foo2.printMessage();

    输出同上。

    注意我们是通过that来访问的message(除了that,context和self也是常用的名称)。Javascript一个还算欣慰的地方就是他的闭包上下文始终是在函数定义的地方,因此不管函数被挂上哪个对象上,捕获到的that始终是这个。当然这个地方不算闭包,有闭无包,但原理是相同的。这也是实践中用的最多的方法,推荐使用。

  • 相关阅读:
    魅族17系列真机谍照泄露 前摄挖孔将添新功能
    联想在S规则债券市场完成了里程碑式的新债券发行
    王小二切饼、马拦过河卒
    Codeforces Round #561 (Div. 2) A Tale of Two Lands 【二分】
    19年省赛后总结
    Winner Winner【模拟、位运算】
    GCDLCM 【米勒_拉宾素数检验 (判断大素数)】
    Floating-Point Hazard【求导公式】
    Communication【floyd判环+并查集】
    Largest Allowed Area【模拟+二分】
  • 原文地址:https://www.cnblogs.com/narcissu5/p/3601407.html
Copyright © 2011-2022 走看看