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 7848955
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T18:22:54+00:00 2026-06-02T18:22:54+00:00

I am learning javascript and the module pattern, but I made a mistake in

  • 0

I am learning javascript and the module pattern, but I made a mistake in my code and it proved wrong some of the concepts I though were true about this pattern. My basic code is like this:

(function(window,$){

//global menu object
var menu = (function(){

    //menu tab component
    var tab = (function(){

        //public properties
        var tab = {
            init:doStuff
        }

        //private properties
        function doStuff(){
            alert("initialising Tab")
        }

        //return public properties
        return tab;
    })();

    //menu curtain component
    var curtain = (function(){

        //public properties
        var curtain = {
            init:doStuff
        }

        //private properties
        function doStuff(){
            alert("initialising Curtain")
        }

        //return public properties
        return curtain;
    })();

    //menu content component
    var content = (function(){

        //public properties
        var content = {
            init:doStuff
        }

        //private properties
        function doStuff(){
            alert("initialising Content")
        }

        //return public properties
        return content;
    })();

    //public properties
    var menu = {
        init:initialiseMenu
    }

    //private properties
    function initialiseMenu(){
        //initialise each component of the menu system
        tab.init();
        curtain.init();
        content.init();

    }

    //final menu object
    return menu;
})();

window.menu = menu;
})(window,jQuery);

Then When my page loads and the code is called:

menu.init();

It gives the alerts in order:

initialising tab
initialising curtain
initialising content

As I expect. But if I change the content component to be like this:

   //menu content component
    var content = (function(){

        //public properties
        var content = {
            init:doStuff
        }

        //private properties
        function doStuff(){
            alert("initialising Content")
        }

        //CHECK ACCESS TO PREVIOUS VARIABLES
        curtain.init();

        //return public properties
        return content;
    })();

it gives out the alerts in order:

initialising curtain
initialising tab
initialising curtain
initialising content

So I see that it is able to access the curtain variable even though it wasn’t passed into the module as an argument.
I thought that each module would be self contained but I have discovered that this is not the case, Is there anyway to make a module only have access to variables you want it too? in particular to my example would be helpful,
Thanks Dan.

  • 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-06-02T18:22:55+00:00Added an answer on June 2, 2026 at 6:22 pm

    Each module is not self contained, rather, it creates a new scope that is a superset of the one in which it was created. The only thing that defines a new scope in Javascript is the function statement. Within a new scope, everything from the outer scope is visible unless overridden by a variable of the same name. Nothing in the inner scope is visible to something outside it.

    var global;
    function outer() {
        var outerVar;
    
        function inner() {
            var innerVar;
    
            // global, outerVar, and innerVar are visible
    
        }   
        function inner2() {
            var inner2var, outerVar;
    
            // global and inner2var are visible
            // outerVar hides the previous outerVar, which is no longer accessible
    
        }
    
        // global and outerVar (the first one) are visible
    
    } 
    

    The fact that your functions are self-executing doesn’t make a difference. Anything created in an outer scope will be visible in your inner scope, unless your create a new local variable of the same name which supercedes it.

    As far as your inner scope is concerned, anything that was created outside it is much the same as a global. (And a global is just a variable created in the default scope, “window” in a browser).

    You could think of an inner scope like being behind one-way glass. You can still see everything in the world, but the world can’t see you. And you can always choose to block the one-way glass so you can no longer see out. But nothing will ever be able to see in.

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

Sidebar

Related Questions

I'm just messing around learning about JavaScript and I wanted to change the color
I have been studying as much as I can about the Module Pattern suggested
I started learning JavaScript last week, I saw this code yesterday and I did
I'm learning Javascript via an online tutorial, but nowhere on that website or any
I'm only just learning javascript so I imagine this is relatively simple but it's
I'm learning javascript for a new project. I have seen many tutorials about javascript
I am learning javascript and can someone please explain the following code snippet for
I'm having a tough time learning Javascript/Ext-JS. I setup the Spket plug-in for some
I've been broadening my horizons learning javascript and I have a quick question about
Started learning javascript yesterday at CodeAcademy and decided I could actually write code. Just

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.