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

135
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
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