箭头函数不会修改this

时间:2022-06-01
本文章向大家介绍箭头函数不会修改this,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
function Person () {
  this.name = 'little bear',
  this.age = 18
  setTimeout(()=>{
	console.log(this
)
})
}
var a = new Person()

setTimeout里的this代表new Person出来的对象

function Person () {
  this.name = 'little bear',
  this.age = 18
  setTimeout(function(){
	console.log(this
)
})
}
var a = new Person()

setTimeout里的this代表window

由于箭头函数不改变this,所以setTimeout里面的箭头函数this,不能改变