Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7852237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T19:22:26+00:00 2026-06-02T19:22:26+00:00

I have a name of a method as a string in javascript variable and

  • 0

I have a name of a method as a string in javascript variable and I would like to get a result of its call to variable:

var myMethod = "methodToBeCalled";
var result;
eval("result = "+myMethod+"();")

This works and there are no problems. But this code is inacceptable for Google Closure Compiler. How can I modify it to work with it? Thanks!

EDIT:

It seems the proposed solutions does not work when the name of the method is inside of some object, for instance:

var myFunction = function () { return "foo!" }
var myObject = {
    itsMethod: function() { return "foo!" }
};
...
var fstMethodToCall = "myFunction"
var sndMethodToCall = "myObject.itsMethod";
...
window[fstMethodToCall](); // foo!
window[sndMethodToCall](); // undefined
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T19:22:28+00:00Added an answer on June 2, 2026 at 7:22 pm

    Assuming you are not in a nested scope of some kind, try:

    var result = window['methodToBeCalled']();
    

    or

    var myMethod = 'methodToBeCalled';
    var result = window[myMethod]();
    

    To execute an arbitrary function of arbitrary depth based on a string specification, while not executing eval:

    var SomeObject = {
        level1: {
            level2: {
                someFunc: function () {
                    console.log('hello');
                }
            }
        }
    };
    
    var target = 'SomeObject.level1.level2.someFunc';
    
    var obj;
    var split = target.split('.');
    for (var i = 0; i < split.length; i++) {
        obj = (obj || window)[split[i]];
    }
    
    obj();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have written a method in java like this public boolean ADD(String ID,String Name,String
I have wriiten a method like this public ArrayList<T> GetDoctorDetail(String name) { if (name!=null
I have this in AS3 var myName:String = David; var result:Number = ExternalInterface.call(methodInJS, myName);
I have the C# method private static string TypeNameLower(object o) { return o.GetType().Name.ToLower(); }
I have this method for grabbing the file name from a string URI. What
I have a method with the following signature: public ActionResult RenderFamilyTree(string name, Dictionary<string, string>
In java can an instance variable and a method have the same name without
I would like to have 4 actions with the same name (controller methods may
I have the following JavaScript code: var name = ui.item.label.split('. ', 2); name.lastname =
I have the name of a function in JavaScript as a string. How do

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.