const list = {
name: 'Harry Potter',
birthYear: 1995,
calcAge: function () {
this.age = 2002 - this.birthYear;
return this.age;
}
};
console.log(list.age);

0 Answer
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);
the calcAge
is function property so you must be treated as a function list.calcAge()
const list = {
name: 'Harry Potter',
birthYear: 1995,
calcAge: function () {
this.age = 2002 - this.birthYear;
return this.age;
}
};
document.getElementById("name").innerHTML = list.name;
document.getElementById("birthYear").innerHTML = list.birthYear;
document.getElementById("age").innerHTML = list.calcAge();
Call your function. list.calcAge()
You can print it like this console.log(list.calcAge())
You were close though!!
这家伙很懒,什么都没留下...