js:关于this的问题

关于this的问题

this指向调用它的对象:

    var name = "The Window";//全局变量
  var object = {
    
    
    name : "My Object",
    getNameFunc : function(){
    
    //它是object直接调用的,所以this指向object
        var that = this;//这里的this为object
      return function(){
    
    //它是window直接调用的,所以this指向window
        return this.name;//这里的this为window
        //return that.name;//这里的that为object
      };
    }
  };
  alert(object.getNameFunc()());

猜你喜欢

转载自blog.csdn.net/m0_49888984/article/details/115439344