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

150
Views
How to execute a function from the value of a map object

I want to be able to execute a function which is saved as a value on Map object by referencing the key. For example:

const myMap = new Map();

myMap.set("myKey", function(argument) {
    console.log(argument);
});

let value = myMap.get("myKey");
console.log(value);

The function is output to the console but I can't figure out how to actually execute the function.

I tried let test = new Function(value) but got the error message "Function statements require a function name"

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Invoke the function with parentheses:

const myMap = new Map();

myMap.set("myKey", function(argument) {
    console.log(argument);
});

let value = myMap.get("myKey");
value('Hello World!')

about 4 years ago · Juan Pablo Isaza Report

0

As get method of myMap will give you the function, You just have to invoke it as

myMap.get("myKey")("Hello World!")

const myMap = new Map();

myMap.set("myKey", function (argument) {
  console.log(argument);
});

let value = myMap.get("myKey")("Hello World!");

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!