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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:12:50+00:00 2026-05-26T02:12:50+00:00

Possible Duplicate: javascript create INcode workspace (framework) In the last few days i have

  • 0

Possible Duplicate:
javascript create INcode workspace (framework)

In the last few days i have been interesting in how Javascript framework have they own workspace , what i mean is how they can call a variable at any name they want and work with it without involving local variables.

I’m writing now an API and i’m facing the problem of the local variables / function local names , if they already exist.

So , what i have done is looking into how framework works and i saw they use namespace functions like this :

(function(){ .... })()

so i tried to do something alike

<html>

  <head>
    <script type="text/javascript">
      (function() {
        function f(toAlert) {  alert(toAlert);  }
      })();
    </script>
  </head>
  <body>

    <script>
      toAlert("hi");
    </script>

  </body>
</html>

but it’s not working..

so i have few questions :

  1. how to have a workspace in javascript to set any variable name i want (if function too it would be great).

  2. i saw that the frameworks uses “window” command , is it have something to do with the namespace function to “public” the function or w/e?

  3. i’ll be glad if you can give me information / tutorials about all this thing , how to make it done from beginning to ending

  • 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-26T02:12:51+00:00Added an answer on May 26, 2026 at 2:12 am

    of course it’s not working, because your f(toAlert) function is only visible within the immediately executing function closure scope. And you’re also trying to call some function that doesn’t exists in the first place. Your closure function is called f (and has toAlert parameter), and then you’re trying to call function called toAlert that wasn’t defined anywhere. Not in global nor in closure scope.

    Scope?

    This code may clear things up a bit for you. Read comments.

    <script type="text/javascript">
    
        // global scope
        function globalFunc(text) {
            alert(text);
        }
    
        var privateFunc = null;
    
        // function closure scope
        (function(){
    
            // closure function
            function closureFunc(text) {
                // can call global
                globalFunc(text);
            }
    
            // let's make closure function accessible from global scope
            // since privateFunc variable is in global scope
            privateFunc = closureFunc;
        })();
    
        // call closure function
        privateFunc("calling closure function");
    
        // ERROR: this will not work due to function being in closure scope
        closureFunc("calling closure function");
    
    </script>
    

    Creating local scope

    What does that strange function parentheses actually do?

    This is some function:

    function name(someParameter) { ... }
    

    Putting it in parentheses and adding some at the end, executes it right away:

    (function name(someParameter) { ... })("Parameter text value");
    

    Mind the function parameter…

    Why do libraries use local scope?

    Libraries usually use local scope to not pollute and more importantly clash with other possible libraries. Think of two libraries that would both define a function called getName. The last one that would define it would simply override the first one’s implementation thus making the first library to malfunction.

    When each library creates its own closure scope they can create whatever functions, variables within and use them without the fear of being overridden. Libraries usually just expose some small part into global scope, so other scripts can actually use the library.

    (function() {
    
        var closureVar = "I'm local";
    
        globalVar = "I'm global";
        // or
        window.globalVar2 = "I'm global equivalent";
    
    })();
    

    omitting var or referencing window object makes a function or variable global.

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

Sidebar

Related Questions

Possible Duplicate: Escape string for use in Javascript regex I have a msg like
Possible Duplicate: How to create a GUID / UUID in Javascript? I need to
Possible Duplicate: Explain JavaScript's encapsulated anonymous function syntax I have just read a javascript
Possible Duplicate: JavaScript: var functionName = function() {} vs function functionName() {} What's the
Possible Duplicate: JavaScript data formatting/pretty printer I am getting a bit tired of looking
Possible Duplicate: Debugging JavaScript in IE7 Firefox has Web Developer plugin and Firebug for
Possible Duplicate: How can I pre-set arguments in JavaScript function call? (Partial Function Application)
Possible Duplicate: Pass a PHP string to a Javascript variable (and escape newlines) I
Possible Duplicate: What is the best way to do loops in JavaScript What’s the
Possible Duplicate: What’s the difference between “Array()” and “[]” while declaring a JavaScript array?

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.