gpt4 book ai didi

javascript - typescript : fat arrow & this scope relation

转载 作者:搜寻专家 更新时间:2023-10-30 21:19:17 27 4
gpt4 key购买 nike

<分区>

我在练习下面的代码代码引用:Link

   let obj = {
name : "Cj",
sayLater : function(){
setTimeout(function(){
console.log("sayLater :> "+this.name);
},3000)
},
sayNow : function(){
console.log("sayNow :> "+this.name);
},
sayLaterFA : function(){
setTimeout(() => console.log("sayLaterFA :> "+this.name) ,3000)
},
sayNowFA : () => console.log("sayNowFA :> "+this.name),
sayLaterPureFatArrow : () => { setTimeout(() => console.log("sayLaterPureFatArrow :> "+this.name),4000) },
sayNowPureFatArrow : () => { console.log("sayNowPureFatArrow :> "+this.name) }
}



obj.sayLater(); //Output : sayLater :> undefined
obj.sayNow(); //Output : sayNow :> Cj
obj.sayLaterFA(); //Output : sayLaterFA :> Cj
obj.sayNowFA(); //Output : sayNowFA :> Cj
obj.sayLaterPureFatArrow(); //Output : sayLaterPureFatArrow :> undefined
obj.sayNowPureFatArrow(); //Output : sayNowPureFatArrow :> undefined

谁能解释一下为什么我的输出是未定义的,尽管我使用的是粗箭头函数

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com