views:

191

answers:

2

Update2:
What I really wanted to ask was already argued in a different page. Please check the following entry.
(Thanks to BobS.)
http://stackoverflow.com/questions/598878/how-can-i-access-local-scope-dynamically-in-javascript


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 can I call the function bar from a String "bar"?

Thank you for your help.

Update:
Here's what I want:

  1. Define functions that are only used in the closure.
  2. Avoid creating an Object in the closure that holds those functions in order to be accessed as obj['bar'].
  3. Avoid eval (if possible) in order to write code more simply in a straightforward manner (if exists).
  4. Decide function's name dynamically via the URI parameter or anything variable.

Being a newbie of Javascript, I thought 'this' would be the counterpart of 'window' in the closure, and tried writing:

// in the closure
name = 'bar';
this[name]; // undefined ...

and failed (of course...).
All of these are for pursuit of further laziness. Javascript is kind of new to me and currently I've been trying to write code as lazy as possible.

+2  A: 

Inside your ready function:

window.bar = function bar() {
    // ...
}

Then, you can access window['bar'].

Chris Jester-Young
Thank you very much for your prompt reply. I appreciate it.However, I want to define the function bar in the closure, not in a global scope, because I want the function to be confined in the specific function(situation). I'm sorry my explanation was probably not enough. Thank you.
Kai barry yuzanic
Sorry if I'm playing Devil's advocate here, but: if you want to access the function from outside the closure, why define it within the closure? Doesn't that defeat the purpose?
Zack Mulgrew
Any pointing out will be welcome here :) Thank you for your comment. I want to access the function from inside the closure, not outside, by calling the function dynamically from a String of the function's name. But actually, I was also wondering if it's possible to call it from outside the closure :)
Kai barry yuzanic
+2  A: 

As Kobi wrote, eval might be a good option. Alternatively, is there any reason not to do

$(function(){
  var localNamespace = {};
  function bar() {
      alert("bar");
  }
  localNamespace['bar'] = bar;
  // Now bar() can be called by, well, localNamespace['bar']
}

Update: Similar SO entries, such as http://stackoverflow.com/questions/598878/how-can-i-access-local-scope-dynamically-in-javascript, seem to indicate you're out of luck without using one of these two approaches or something even uglier.

BobS
Thank you so much for your comment and advice. I appreciate it.And thank you very much for the url. I tried finding this kind of entry before I posted here but couldn't find any. Knowing that it's impossible to do what exactly I want, I've taken a big step. Thank you so much BobS again.
Kai barry yuzanic