3

Update2:
What I really wanted to ask was already argued in a different page. Please check the following entry.
(Thanks to BobS.)
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.

Community
  • 1
  • 1
Wanien
  • 41
  • 1
  • 5
  • 1
    Eval seems like a good option here. You probably create nested closures anyway, don't you? Can you explain the scenario? Why do you have `bar` as a string? Have you considered defining these functions where you need them? – Kobi Mar 08 '10 at 05:31
  • Thank you so much for your comment and advice. To make it clear, I added what I want in the question board above (below edit). I hope this time my explanation is enough to be understood. Thank you. – Wanien Mar 08 '10 at 06:27

2 Answers2

3

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 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.

Community
  • 1
  • 1
BobS
  • 2,588
  • 1
  • 15
  • 15
  • 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. – Wanien Mar 08 '10 at 07:03
2

Inside your ready function:

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

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

C. K. Young
  • 219,335
  • 46
  • 382
  • 435
  • 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. – Wanien Mar 08 '10 at 05:50
  • 1
    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 The Human Mar 08 '10 at 07:06
  • 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 :) – Wanien Mar 08 '10 at 07:45