我正在学习 Javascript,遇到了一些问题。这是我在这里的第一篇文章,请帮助我理解:
这是我的代码:
const penguin = {
firstName: 'Zachary',
birthYear: 1995,
job: 'Developer',
friends: ['Michael', 'Joe', 'Evan'],
hasDriversLicense: true,
// methods
calcAge: function () {
this.age = 2024 - this.birthyear;
return this.age;
},
getSummary: function () {
this.hasDriversLicense ? this.getSummary = 'a' : this.getSummary = 'no';
return this.getSummary;
}
};
penguin.calcAge();
penguin.getSummary();
console.log(penguin.age); // why NaN ???
console.log(penguin.getSummary);
// summary
console.log(`${penguin.firstName} is a ${penguin.age} year-old ${penguin.job}, and he has ${penguin.getSummary} Driver's License.`);
提前谢谢您。
我尝试登录到我的浏览器控制台
console.log(penguin.age)
,它输出 NaN(但是,如果硬编码 1995 年的数字,我会得到预期结果:29)。
检查 calcAge 函数中 this.birthyear 的拼写,您希望它是 this.birthYear。