如何让post对象的属性在getFullName函数中可用。我知道如果我们可以在箭头函数中使用函数声明,那么'this‘将引用post对象属性。但是我想知道我们是否可以使用箭头函数来实现它。
const post = {
firstname: 'temp',
lastname: 'code',
getFullName: () => {
return this.firstname + ' ' + this.lastname;
}
}发布于 2021-01-18 22:52:58
您可以引用post对象本身。
const post = {
firstname: 'temp',
lastname: 'code',
getFullName: () => {
return post.firstname + ' ' + post.lastname;
}
}
console.log(post.getFullName());
https://stackoverflow.com/questions/65776806
复制相似问题