【发布时间】:2022-01-10 18:22:23
【问题描述】:
当我直接访问年龄属性时,为什么控制台打印未定义。我必须先打印 calcAge 函数才能获取值。
const Osama = {
firstName: 'osama',
lastName: 'Shaikh',
birthyear: 1999,
job: 'developer',
friends: ['michael', 'peter', 'steven'],
haslicense: true,
calcAge: function () {
this.age = 2021 - this.birthyear;
return this.age;
},
};
console.log(Osama.age);
【问题讨论】:
标签: javascript function object javascript-objects