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

133
Visualizações
Javascript Object method value not showing with console.log of the object

I'm a bit confused with objects in JavaScript...

I wrote an object:

const gix = {
  firstName: "John",
  lastName: "Johnson",
  yearOfBirth: 2000,
  profession: "IT",
  friends: ["Mark", "Luke", "John"],
  driversLicence: true,
  age: function () {
    this.calcAge = 2022 - this.yearOfBirth;
    return this.calcAge;
  },
};

gix.age();
console.log(gix);

Why is the console log of the whole object not showing the calculated value but is showing age: f()

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Considering your use-case, you could replace the method with a getter, which gets evaluted each time the object is referenced:

const gix = {
  firstName: "John",
  lastName: "Johnson",
  yearOfBirth: 2000,
  profession: "IT",
  friends: ["Mark", "Luke", "John"],
  driversLicence: true,
  get age() {
    return 2022 - this.yearOfBirth;
  },
};
about 4 years ago · Juan Pablo Isaza Relatório

0

You would either want to capture the return value of the function or call it so:

console.log(gix.age());

Think of age as a pointer to the function...

about 4 years ago · Juan Pablo Isaza Relatório

0

When you're console logging a function it wont execute it, It will just show the contents of the function, IF you need to see the value then you need to call the function and log the output

console.log(gix.age());

about 4 years ago · Juan Pablo Isaza Relatório
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