gpt4 book ai didi

javascript - 为什么 'this' 在箭头函数中引用时返回 'undefined' 而在匿名函数中调用时却不返回?

转载 作者:行者123 更新时间:2023-11-30 08:29:36 24 4
gpt4 key购买 nike

<分区>

我正在使用 node.js v6.7.0 并在声明一个引用“this”的对象时,如果它在箭头函数内则返回未定义,但当它在常规匿名函数内时它返回对象本身(是我想要的)

例如

let obj = {
key: 'val',
getScopeWithArrow: () => {return this;}, //returns undefined
getScopeWithAnonymous: function() {return this;} //returns the object properly
}

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