Hello World

前端

1.this

------------------------------------
1.
function func() {
        //this,代指 window
    } 
    
    func();

2.
function Func() {
        this.name = '葫芦娃'; //this,代指 obj
    }

    obj = new Func();
    console.log(obj.name);

3.
 function Func() {
        this.name = '葫芦娃'; //this,代指 obj
        this.show = function(){
            console.log(this);
        }
    }

    obj = new Func();
    console.show();
            

4.
userInfo = {
       name:'大宝',
       age:18,
       show:function() {
           console.log(this)  //userInfo
       }

   };

   userInfo.show()

#-------结论:函数被 对象.函数  执行,那么函数中的this 就是 该对象   --------#


加强版:
1.
userInfo = {
       name:'小黄人',
       age:20,

       show:function(){
           console.log(this);       //代指 userinfo
           (function(){
               console.log(this);   //代指 window
           })()
       }
   };

   userInfo.show()

2.
 function Func(){
      this.name = '小黄人';
      this.show = function(){
          console.log(this);        //Func----(obj)
          (function () {
              console.log(this);    //window
          })()
      }
  }

  obj = new Func();
  obj.show()
  

3.
userInfo = {
      name:'小黄人',
      age:20,
      show:function(){
          console.log(this);       //userInfo
          
          var that = this;        //!!!!!!
          (function(){
             console.log(that);   //userInfo    
          })()
      }
      
  };
  
  
    userInfo.show()
示例

 

posted @ 2017-11-20 16:51  nayike  阅读(144)  评论(0编辑  收藏  举报

Hello