Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

142
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!