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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T00:55:12+00:00 2026-05-22T00:55:12+00:00

A while ago, I offered-up a JavaScript design pattern (the Module Pattern – see

  • 0

A while ago, I offered-up a JavaScript design pattern (the Module Pattern – see below) that I got from a John Resig example as part of a solution to someone’s question and I received the following comment:

“…that pattern is a bit over
engineered and not that good. Still
leaking into global-scope. and your
not opening yourself to async loaders.
But it is better then just ad-hoc
coding !”

So…

If “leaking” into global scope means “your object gets appended to the browsers window (object)”…then everything already gets appended (globally):

This “leaks” into global scope:

window.jQuery

…just call: window.jQuery and it resolves as a function();

This “leaks” into global scope:

function HelloWorld() { alert(‘Howdy’); }

…just call: window.HelloWorld() and you will get ‘Howdy’.

This “leaks” into global scope:

var myVariable = 10;

…just call: window.myVariable and you will get 10

If the commenter is correct, then all the above “leak” into global-scope. So, personally, I don’t see a way NOT to “leak” into global-scope as even your form controls exists there (as well).

As such, here are my questions…

  • What is meant by “leaking” into
    global-scope?
  • Why is that bad?
  • How do you avoid it?
  • When wanting to create persistent
    custom-objects, why is the Module
    Pattern (below) bad?
  • Design patterns let you encapsulate
    complex logic, is encapsulation
    suddenly bad simply because we’re
    writing in JavaScript
    ?
  • Or…is this commenter simply wrong?

Here is the Module Pattern I Mentioned Above:

<script type="text/javascript">
    var myNamespace = (function($) {
        var publicInstances = {};

        // ***********************
        // myObject
        publicInstances.myObject = myObject;
        function myObject() {

            /// <summary>A pointer to this</summary>
            var self = this;

            this.someProperty = new String();

            this.initialize = function() {
                /// your code here
            }
            this.someMethod = function() {
                /// your code here
            }

            self.initialize();
        }

        return publicInstances;
    })(jQuery);


    jQuery(document).ready(function() {
        // Use would look like
        var myInstance = new myNamespace.myObject();
    });
</script>

UPDATED:
I’m satisfied with the answers below and want to thank everyone for taking the time to comment.

TO RECAP THE ANSWERS BELOW:
“Leaking” into global-scope occurs when something used in local-scope is unintentionally made available to the global-scope (e.g. the window object). This is bad because it opens the page to potential naming collisions which could result in variables resolving to unexpected values or types.

Intentionally making a variable global is not considered a “leak”. However, properly namespacing the object is required to reduce potential for said naming collisions.

You cannot avoid globally-scoped variables, but you can reduce the above risks by using asynchronous-loaders and defining-modules made available in plug-ins like RequireJS or Curl.

  • 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-22T00:55:12+00:00Added an answer on May 22, 2026 at 12:55 am

    [[Short story]]

    Don’t make global variables ever and use an async module loader like requirejs or curl

    [[Long story]]

    That comment was poorly structured.

    There is nothing wrong with the module system. I was complaining about using global variables at all. (I still think the full generic module pattern is bloated).

    Whether you should avoid all global variables is a different question and I think a matter of style. You can either use an async loader to pass modules around or using window to pass modules around.

    • What is meant by “leaking” into global-scope?

    What I meant was your creating global variables. Minimising the use of global variables is a pattern. In functional style programming it’s possible to have zero global variables but this is a different pattern from using global modules.

    • Why is that bad?

    Having any state globally can cause that state to be corrupted.

    • How do you avoid it?

    You can’t. You can minimize the amount of global variables though. To avoid having global state completely you can use asynchronous loaders. These define a few global variables for you that you can then use.

    • When wanting to create persistent custom-objects, why is the Module Pattern (below) bad?

    There is nothing wrong with the module pattern. The problem is storing your module globally. The issue is having global namespaces.

    • Design patterns let you encapsulate complex logic, is encapsulation suddenly bad simply because we’re writing in JavaScript?

    Now that I’ve cleared up the intent of the comment this question isn’t really relevant

    • Or…is this commenter simply wrong?

    The comment was poorly phrased at best. I objected to global namespaces rather than modules, but did not state this properly.

    The alternative is using asynchronous loaders and defining modules. These can be narrowed down to two global variables. define and require.

    require = function(moduleName, callback)

    This will get a module and then return it to you.

    define = function(obj)

    this defines a module.

    The concept here is that you multi file code as follows:

    // main.js
    require([
      "foo.js",
      "bar.js",
      ...,
    ], function(foo, bar, ...) {
       // do stuff
    }); 
    
    //foo.js
    
    (function() {
        var namespace = modulePatternCode;
        ...
        define(namespace):
    })();
    
    //bar.js 
    
    (function() {
        var namespace = modulePatternCode;
        ...
        define(namespace):
    })();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A while ago, I came across some code that marked a data member of
A while ago I had a query that I ran quite a lot for
A while ago, I asked a question about $ , and got useful answers
A while ago another question referred to the (possibly urban tale) statistic that ...
A while ago, I saw on a Screencast a tool that could be ran
A while ago, I wrote a web-based guestbook application that wrote it's own database.
A while ago I found out that FOP doesn't allow you to use floats,
A while ago I remember seeing a constant of some kind that defined the
A while ago I read a news here , that Microsoft changed its strategy
A while ago I found a packaging tool for visual studio that allowed quite

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.