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

112
Views
Using JavaScript reflection to invoke a function

I am new to JavaScript and am struggling with how reflection works there. What I want to do is: Given a String which is the function's name, invoke that function. E.g. var myString = "myFunctionName()" is given and then I want to invoke myFunctionName() using myString somehow.

Is this possible and how?

Thanks in advance!

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

0

It all depends on the context in which the function is declared.

For the browser's window context:

function test() {
  console.log('Hello world!');
}

const string = 'test';

window[string]();

about 4 years ago · Juan Pablo Isaza Report

0

You can try:

function myFunctionName() {
  // do something...
}

var myString = "myFunctionName()";

eval(myString) 
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!