javascript - 对象字面量中的全名
问题描述
我正在尝试使用对象文字中的函数来获取全名,但失败了。我使用的代码如下所示。我可能做错了什么?
fullName function(){
return this.firstName +" "+this.lastName;
}
console.log(person.fullName());
解决方案
如果你想使用 fullName() 作为 person 对象的方法,你首先需要一个 person 对象。
const person = {
firstname: "john",
lastname: "doe",
fullName() {
return `${this.firstname} ${this.lastname}`
}
}
console.log(person.fullName());
推荐阅读
- mysql - 从表中删除部分重复的行
- css - 使用 Preact Build 时如何使样式标签与模板 HTML 保持一致?
- tabs - Nativescript 8.1 无法识别
- javascript - 单击按钮一次单击仅呈现文本
- c - 这是否完全释放了两个结构
- bash - 为什么相同的字符有不同的字节表示以及如何转换
- android - 无法解析 activitycompat 和 appcompatactivity
- python - 为什么我得到一个错误,但我仍然得到正确的输出,有没有人有解决方案?
- java - Coinbase 分页返回 {"errors":[{"id":"not_found","message":"Not found"}]}
- python - 将类对象列表保存到 JSON 文件中,以便以后读取