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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:09:39+00:00 2026-05-13T23:09:39+00:00

Update2: What I really wanted to ask was already argued in a different page.

  • 0

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.

  • 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-13T23:09:39+00:00Added an answer on May 13, 2026 at 11:09 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm really confused. I want to make a Chrome Extension that can update a
[update2] One of those problems I solved & can't remember how... :) [update1] could
UPDATE: After some additional reading, what I really wanted was guaranteed early binding (which
Update: What I really wanted all along were greenlets . Note: This question mutated
Here’s a really really super-basic question. Say I wanted to build – today –
Not really done this before so I wanted a few pointers. In my app,
There is a question very similar to this but I wanted to ask it
We recently updated our Tomcat web service. The only things we really updated were
UPDATE So totally pulled a tool moment. I really meant by reference versus Out/Ref.
Update: Now that it's 2016 I'd use PowerShell for this unless there's a really

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.