尝试重新学习 JS 的初学者。下面是我的以下代码。我用 calculateAge 方法创建了一个 persons 类。当试图将我的新 const 传递到我的方法时,我得到一个未定义的,为什么会这样?请指教:
class Person6 {
constructor (name, yearOfBirth, job) {
this.name = name;
this.yearOfBirth = yearOfBirth;
this.job = job;
}
// Functions can directly add to the class
calculateAge() {
var age = new Date().getFullYear - this.yearOfBirth;
console.log(age);
}
// Static classes
static greeting() {
console.log('Hey there!');
}
}
const quang6 = new Person6('Quang', 1994, 'IT');
Person6.greeting();
quang6.calculateAge();
墨色风雨
相关分类