关于函数中this的指向问题

113 阅读1分钟

关于函数中this的指向问题

1.普通函数中:

function fn(){
console.log(this)    //this指向window
}

2.对象方法中:

xxx.方法名=function(){
console.log(this)    //this指向xxx
}

3.构造函数中:

new 函数名()   //this指向new创建的实例对象

4.箭头函数中:

let fu=()=>{
console.log(this)   //箭头函数本质是访问上级作用域中的this
}

总结

  • 没点没new是window,有new是实例,有点是点左边的对象