I want to assign property age to the object and I know it can be done using a function like:
var john = {
firstName: "John",
lastName: "Smith",
job: "Teacher",
yearOfBirth: 1990,
family: ['Jane', 'Mark', 'Bob'],
calculateAge: function() {
this.age = 2018 - this.yearOfBirth
}
}
john.calculateAge();
It adds property age into the object John as expected. But can it be done without a function?
When I tried to do like following:
var john = {
firstName: "John",
lastName: "Smith",
job: "Teacher",
yearOfBirth: 1990,
family: ['Jane', 'Mark', 'Bob'],
age: 2018 - this.yearOfBirth
}
"age" property returns "NaN". I don't know why it is not returning a number. Is it regular behavior (if 'yes', then why?) or am I doing something wrong over here?