JS笔记

  • function 中的this指向问题
JS 中this指向问题
//undefined
var user = {
    count:1,
    getCount:function(){
      return this.count;   
    }
}
console.log(user.getCount()); //1
var otherGetCount = user.getCount;
console.log(otherGetCount());  //undefined;

//count为全局时的,this可以访问到var中的this
var count = 2;
var user = {
    count:1,
    getCount:function(){
      return this.count;   
    }
}
console.log(user.getCount()); //1
var otherGetCount = user.getCount;
console.log(otherGetCount());  //2;