箭头函数的this和普通函数的this

参考博文:https://www.cnblogs.com/fanzhanxiang/p/8888963.html

箭头函数本身没有this,所以它内部的this是继承自父执行上下文中的this。所以以下调用obj调用func0()的时候,this是window,父级执行上下文是window

name = "window";
var obj = {
name: "obj",
func0: () => {
    console.log(this)
    console.log(this.name);
 },
func1: function() {
    console.log(this)
    console.log(this.name);
 },
}
obj.func0()

猜你喜欢

转载自blog.csdn.net/qq_41083105/article/details/116062537
今日推荐