Skip to content

Instantly share code, notes, and snippets.

@quangkeu95
Last active November 5, 2018 04:26
Show Gist options
  • Save quangkeu95/664300fd16db283b5e6e2700bf9a5464 to your computer and use it in GitHub Desktop.
Save quangkeu95/664300fd16db283b5e6e2700bf9a5464 to your computer and use it in GitHub Desktop.
function User(name, birthday) {
this.name = name;
this.birthday = birthday;
// age is calculated from the current date and 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 ); // birthday is available
console.log( john.age ); // ...as well as the age
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment