Javascript: Calling a function written in an anonymous function from String with the function's name

Posted by Kai barry yuzanic on Stack Overflow See other posts from Stack Overflow or by Kai barry yuzanic
Published on 2010-03-08T05:14:02Z Indexed on 2010/03/08 5:21 UTC
Read the original article Hit count: 458

Filed under:
|
|
|
|

Hello.

I've started using jQuery and am wondering how to call functions in an anonymous function dynamically from String. Let's say for instance, I have the following functions:

function foo() {
 // Being in the global namespace, 
 // this function can be called with window['foo']()
  alert("foo");
}

jQuery(document).ready(function(){
  function bar() {
    // How can this function be called 
    // by using a String of the function's name 'bar'??
    alert("bar");
  }

  // I want to call the function bar here from String with the name 'bar' 
}

I've been trying to figure out what could be the counterpart of 'window', which can call functions from the global namespace such as window["foo"]. In the small example above, how I can call the function bar from a String "bar"?

Thank you for your help.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about lambda