前端this指向大全

mdn官方解释链接

点击此处跳转

总结:

  1. 全局环境的this指向window
  2. 箭头函数的this指向函数创建时指的this,普通函数随着被调用所处的环境不同this指向会发生变化
  3. 对象里的普通函数指向被调用的对象

同时也可以参考下面这个链接
https://javascript.ruanyifeng.com/oop/this.html
总结:
this就是属性或方法“当前”所在的对象。

箭头函数普通函数的区别:

var a ={
    
    
test:function(){
    
    
console.log(this)
},
test1:()=>{
    
    
console.log(this)
}
}
a.test(); 
// Object { test: test(),test1:test1() }
a.test1(); 
// output window

猜你喜欢

转载自blog.csdn.net/qq_26889291/article/details/108748127
今日推荐