Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

339
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda