0

I want to modify a method based off a method in the same object.

const date = new Date();
const year = date.getYear();
personData = {
   birthdayYear: 2000,
   age: year - personData.birthdayYear,
}
console.log(personData.age); // should return 22

How could I make this work?

  • 2
    You need to change `date.getYear()` **to** `date.getFullYear()`; . Then try using getter method `const personData = { birthdayYear: 2000, get age(){ return year - this.birthdayYear }}` – Maniraj Murugan Apr 28 '22 at 09:50
  • @Yogi but personData.age just gets saved as that function, instead of what it returns. – The Code Challenger Apr 28 '22 at 09:56
  • I think your reply was for @ManirajMurugan who provided the correct answer. Also, see this SO question which might help: [Computed Property in JavaScript](https://stackoverflow.com/questions/56144730/computed-property-in-vanilla-javascript-class) – Yogi Apr 28 '22 at 10:04

0 Answers0