this

作者: daisx | 来源:发表于2017-05-18 15:30 被阅读0次

    一、在一般的函数中this指全局对象。

     var x = 1;
        function f(){
            console.log(this.x);
            console.log(this);
        }
        f();
    

    二、最为对象的方法来调用,指调用它的对象。

    var o = {x:1};
      function test(){
          console.log(this.x);
          console.log(this);
      }
      console.log(o.m = test);
      console.log(o.x);
      o.m();
    

    三、构造函数调用,指用new创造出来的对象。

     function Fu(){
            this.x=1;
        }
        var o = new Fu();
         console.log(o.x); 
    

    相关文章

      网友评论

          本文标题:this

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