JS箭头函数的this

箭头函数的this看定义他的时候,他的外层有没有函数

有:外层函数的this就是箭头函数的this

无:箭头函数的this就是window

obj = {age:18, getAge: ()=>console.log(this.age)}obj.getAge()//undefined 定义的时候外层没有函数,指向windowobj = {age:18, getAge: function(){print = ()=>console.log(this.age); print()}}obj.getAge()//18 定义的时候外层函数的this就是箭头函数的this

  

相关文章