I'm trying to build muscle memory by using arrow functions, but I seem to be having a conflict with using one inside of an object. Here's my code:
const gwen = {
firstName : 'Gwen',
lastName : 'Stacy',
mass : 78,
height : 1.69,
calcBMI: function () {
return this.mass / (this.height ** 2)
}
}
const patrick = {
firstName : 'Patrick',
lastName : 'Arnolds',
mass : 92,
height : 1.95,
calcBMI: () => this.mass / (this.height ** 2)
}
console.log(gwen.mass, gwen.height, gwen.calcBMI())
console.log(patrick.mass, patrick.height, patrick.calcBMI())
Output from the first object called 'gwen' uses a regular function definition:
78 1.69 27.309968138370508
Output from the second object called 'patrick' is my attempt at using an arrow function inside of an object:
92 1.95 NaN
I've looked up articles on other websites from a search, but it seems nothing has been written based on my specific issue. I'm following along with tutorials and experimentation on the side. Maybe I'm attempting to use an arrow function in the wrong context.