const list = {
name: 'Harry Potter',
birthYear: 1995,
calcAge: function () {
this.age = 2002 - this.birthYear;
return this.age;
}
};
console.log(list.age);
You are storing a function, not calling it that's why it should print you undefined. To fix it you should call it with list.calcAge().
Your code works and this.age = 2002 - this.birthYear creates the property you want, you just need to call it before using it.
const list = {
name: 'Harry Potter',
birthYear: 1995,
calcAge: function () {
this.age = 2002 - this.birthYear;
return this.age;
}
};
list.calcAge()
console.log(list.age);
Call your function. list.calcAge() You can print it like this console.log(list.calcAge()) You were close though!!
const list = {
name: 'Harry Potter',
birthYear: 1995,
calcAge() {
this.age = 2002 - this.birthYear;
return this.age;
},
};
console.log(list.calcAge());