Accessing Global Function Pointer from its Name

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • anoop.kn@gmail.com

    Accessing Global Function Pointer from its Name

    Is there anyway to access Global Function Pointer from its name ?
    I want to call the function at runtime during a script execution.
    PS: eval() works, I looking for a more efficient way of doing the
    same !

    Ex:

    function testfunction ( msg )
    {
    alert( msg );
    }

    function onclickCallback ( )
    {
    var fname = "testfuncti on";
    /*
    I want to call the function testfunction here by having access to its
    name
    without the use of eval. Is it possible ?
    */
    }

  • Joost Diepenmaat

    #2
    Re: Accessing Global Function Pointer from its Name

    anoop.kn@gmail. com writes:
    Is there anyway to access Global Function Pointer from its name ?
    I want to call the function at runtime during a script execution.
    PS: eval() works, I looking for a more efficient way of doing the
    same !
    Since global functions are properties of the global object, and the
    global object is "window" (at least in browsers),
    window[functionname](); should work.

    That leaves us with the question of why you'd need the name at all since
    you can refer to functions directly (which /should/ be more efficient):

    function Bla() {
    alert();
    }

    var f = Bla;

    f();

    --
    Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/

    Comment

    • Stevo

      #3
      Re: Accessing Global Function Pointer from its Name

      anoop.kn@gmail. com wrote:
      function testfunction ( msg )
      {
      alert( msg );
      }
      >
      function onclickCallback ( )
      {
      var fname = "testfuncti on";
      /*
      I want to call the function testfunction here by having access to its
      name
      without the use of eval. Is it possible ?
      window[fname]("hello world");

      Comment

      • anoop.kn@gmail.com

        #4
        Re: Accessing Global Function Pointer from its Name

        On Feb 13, 1:47 am, Stevo <ple...@spam-me.comwrote:
        anoop...@gmail. com wrote:
        function testfunction ( msg )
        {
        alert( msg );
        }
        >
        function onclickCallback ( )
        {
        var fname = "testfuncti on";
        /*
        I want to call the function testfunction here by having access to its
        name
        without the use of eval. Is it possible ?
        >
        window[fname]("hello world");
        Thanks a ton fellas !!!

        Comment

        Working...