Answers for "getter setter for age problem"

0

getter setter for age problem

function User(name, birthday) {
  this.name = name;
  this.birthday = birthday;

  Object.defineProperty(this, "age", {
    get() {
      let todayYear = new Date().getFullYear();
      return todayYear - this.birthday.getFullYear();
    }
  });
}

let john = new User("John", new Date(1992, 6, 1));

console.log(john.birthday);
console.log(john.age);
Posted by: Guest on February-25-2021

Browse Popular Code Answers by Language