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

202
Views
Declare function with property in one statement?

I know that I can create a function:

const fn = () => {}

And I know that I can attach a property to it:

fn.a = 'a'

Can I use some kind of object literal syntax to do what I did above in one statement?

I'm thinking something along the lines of:

const fn = {
  (): {},
  a: 'a'
}
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You could do it in one statement like this.

const fn = Object.assign(() => {}, { a: 'a' });

about 4 years ago · Juan Pablo Isaza Report

0

How about we make a little utility function to accomplish that?

function createFunc(func, props) {
  Object.keys(props).forEach((key) => (func[key] = props[key]));
  return func;
}

const f = createFunc(() => console.log("hello"), { a: "A", b: "B" });

f();
console.log(f.a);
console.log(f.b);

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!