函数this三种指向
环境对象this : 谁调用函数,this就指向谁
-
普通函数 : 函数名() this->window
-
对象方法 : 对象名.方法() this->对象
-
构造函数 : new 函数名() this->new创建的实例对象
小技巧:没点没new是window,有new是实例,有点是对象
//作用域链
let obj = {
name: "张三",
eat: function() {
//1级链
console.log(this) //1.obj
function fn() {
//2级链
console.log(this) //2.window
}
fn()
}
}
let eat = obj.eat
obj.eat()