嘗試重新學習 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();
為什么在嘗試調用我的方法時會出現未定義的情況?
catspeake
2022-12-09 15:36:45
