this引用的是函数执行的环境对象。
1、在函数外部使用this,this指的就是window对象。
2、在函数内部使用this,根据函数调用方式,分为3种情况:
- 普通调用,this指的是window对象。【不管是在函数外还是函数内调用】
函数名(); - 函数被new调用,this绑定的是新创建的对象。
- 函数作为某个对象的方法被调用,this指的是这个上级对象。
function test1(){
console.log(this.x); //输出3000
console.log(this); //输出obj1
function test2(){
this.x=500;
console.log(this.x); //输出500
console.log(this); //输出window
}
test2(); //此处test2为普通调用,test2中的this指window对象
console.log(this.x); //输出3000
console.log(this); //输出obj1
}
var obj1={};
obj1.x=3000;
obj1.m=test1; //此处test1作为对象方法被调用,test1中的this指obj1
obj1.m();

3331

被折叠的 条评论
为什么被折叠?



