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

148
Vistas
Result of function, as value in object

Is possible to obtain in sum result of a function ? Not function itself, just result.

let a = 1;
let b = 2;

let object = {
    dummy: 'dummy text',
    sum: function () {
        a + b
    },
}

console.log(object) // { dummy: 'dummy text', sum: [Function: sum] }

I want to obtain : { dummy: 'dummy text', sum: 3 } I don't want to use an external function

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

If you want a simple static value, you could simply assign a + b as the value for sum. Otherwise you'd need to invoke sum method call.

let a = 1;
let b = 2;

let object = {
    dummy: 'dummy text',
    sum: a + b,
}

console.log(object) // { dummy: 'dummy text', sum: 3 }

about 4 years ago · Juan Pablo Isaza Denunciar

0

You just need to execute the function instead of initializing it.

This can be done by instantiating the function before and then call it when creating the object.

let a = 1;
let b = 2;

const sum = (a, b) => a + b

let object = {
    dummy: 'dummy text',
    sum: sum(a, b)
}

console.log(object)

about 4 years ago · Juan Pablo Isaza Denunciar

0

Use an IIFE if you want to assign the result of a function invocation to sum

let a = 1;
let b = 2;

let object = {
  dummy: 'dummy text',
  sum: (function () {
    return a + b;
  })(),
};

console.log(object);

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