分析this的问题,首先要分析this是那个函数
1.函数调用模式:this指向window
function fn() {
‘user strict’ //严格模式下函数调用模式的this是undefined
console.log(this) //window
}
fn()
2.方法调用模式:this指向调用方法的对象
obj = {
name:'xiaoming'
say:function() {
console.log(this) //obj
}
}
obj.say()
3.构造函数调用模式:this指向实例对象
function Person(){
console.log(this) //p
}
p = new Person()
4.上下文调用模式:借用方法调用模式
有call apply bind,this指向被借用方法内部this的指向,也就是那个对象来借用这个方法