I have an object which looks like this:
let john = {
firstName: 'John',
lastName: 'Smith',
fullname: `${this.firstName} ${this.lastName}`,
height: 1.95,
mass: 92,
calcBMI: function () {
this.BMI = this.mass / this.height ** 2;
return this.BMI
}
}
Whenever I look at the fullName
property, it just returns undefined
. But when I type it out manually in the object (i.e fullName: 'John Smith'
), I get the expected result: John Smith.
I looked through MDN web docs, but only found a few sentences on how an expression such as this.myProperty
could return undefined if strict mode was activated, which it was in my case. But even when it was deactivated, the same problem occured.
Could anyone help me out?
P.S: I'm no professional coder, so it could just be me being a fool.
Edit: How I called fullName and one more doubt about my object
I 'm logging the fullName
property to the console, if you were wondering.
One more thing - You may have noticed the calcBMI
function in the object and that it also uses the this
keyword. I'm wondering why it's working in there but not outside.