I was practicing creating an object in JS and created the following object:
const jonas = {
firstName: 'Jonas',
lastName: 'Schmedtmann',
birthYear: 1991,
job: 'teacher',
friends: ['Michael', 'Peter', 'Steven'],
hasDriversLicense: false,
calcAge: function () {
this.age = 2037 - this.birthYear;
return this.age;
},
};
A strange thing happen when I want to print the output of the function calcAge with the following code:
console.log(jonas.age);
The browser outputs "undefined" for the console.log() command.
Can anyone tell me where I got it wrong?
Thanks!
jonas.calcAge()before theconsole.log