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

  • Home
  • SEARCH
  • 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 4033810
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:53:36+00:00 2026-05-20T11:53:36+00:00

I want to be able to pass a reference to an arbitrary function by

  • 0

I want to be able to pass a reference to an arbitrary function by name to another javascript function. If it’s just a global function, there is no problem:

function runFunction(funcName) {
   window[funcName]();
}

But suppose the function could be a member of an arbitrary object, e.g.:

object.property.somefunction = function() {
 //
}

runFunction("object.property.somefunction") does not work. I know I can do this:

window["object"]["property"]["somefunction"]()

So while could write code to parse a string and figure out the heirarchy this way, that seems like work 🙂 So I wondered if there’s any better way to go about this, other than using eval()

  • 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-05-20T11:53:37+00:00Added an answer on May 20, 2026 at 11:53 am

    Nope — other than using eval I do not know another way of walking down an object tree in JavaScript than to build a walker — fortunately, it is easy to do:

    /**
    * Walks down an object tree following a provided path. 
    * Throws an error if the path is invalid.
    * 
    * @argument obj {Object} The object to walk.
    * @argument path {String} The path to walk down the provided object.
    * @argument return_container {Boolean} Should walk return the last node *before* the tip 
    * (i.e my_object.my_node.my_other_node.my_last_node.my_attribute)
    * If `true` `my_last_node` will be returned, rather than the value for `my_attribute`.
    * @returns {Mixed} Object or the value of the last attribute in the path.
    */
    function walk_path(obj, path, return_container) {
        return_container = return_container || false;
        path = path_to_array(path, ".");
        var ln = path.length - 1, i = 0;
        while ( i < ln ) {
            if (typeof obj[path[i]] === 'undefined') {
                var err = new ReferenceError("The path " + path.join(".") + " failed at " + path[i] + ".");
                err.valid_path = path.slice(0,i);
                err.invalid_path = path.slice(i, ln+1);
                err.breaking_point = path[i];
                throw err;
            } else {
                var container = obj;
                obj = obj[path[i]];
                i++;
            }
        }
        // If we get down to the leaf node without errors let's return what was asked for.
        return return_container ? container : obj[path[i]];
    };
    
    /**
    * path_to_array
    *
    * @argument path {string} A path in one of the following formats:
    * `path/to/file`
    * `path\\to\\file`
    * `path.to.file`
    * `path to file`
    * @argument sep {string} optional A seperator for the path. (i.e. "/", ".", "---", etc.)
    *
    * If `sep` is not specified the function will use the first seperator it comes across as the path seperator.
    * The precedence is "/" then "\\" then "." and defaults to " "
    *
    * returns {Array} An array of each of the elements in the string ["path", "to", "file"]
    */
    function path_to_array(path, sep) {
        path = is_string(path) ? path : path + "";
        sep = is_string(sep) ? sep : 
                    path.indexOf("/") >= 0 ? "/" : 
                    path.indexOf("\\") >= 0 ? "\\" : 
                    path.indexOf(".") >= 0 ? "." : " ";
        /* Alternately use SLak's
        sep = is_string(sep) ? sep : /[ \\/.]/;
        if you want simpler code that will split on *any* of the
        delimiters by default. */
        return path.split(sep);
    }
    
    function is_string(s) { return typeof s === "string"; }
    

    Then just call it like so:

    walk_path(my_object, "path.to.my.function")();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Summary I want to be able to call a JavaScript function from a Flex
I want to be able to pass link to view from controller. Is there
I want to be able to pass something into an SQL query to determine
I want to create a string and pass it by reference such that I
I want to be able to capture the exception that is thrown when a
I want to be able to do: For Each thing In things End For
I want to be able to play sound files in my program. Where should
I want to be able to make an HTTP call updating some select boxes
I want to be able to view a SQL Server 2005 Reporting Services report
I want to be able to do this. MyInterface interface = new ServiceProxyHelper<ProxyType>(); Here's

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.