Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

336
Vistas
Using arrow function inside a JavaScript Object?

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.

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda