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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:26:46+00:00 2026-05-25T22:26:46+00:00

I am trying to organize my JavaScript better. My goal is to have modular

  • 0

I am trying to organize my JavaScript better. My goal is to have modular architecture that I can break into separate files (sitename.js, sitename.utils.js etc).

I’d like to know what are advantages and disadvantages of these two patterns and which one is more suitable for breaking into modules that live in separate files.

PATTERN #1 (module pattern)

var MODULE = (function () {

    //private methods

    return {
        common: {
            init: function() {
                console.log("common.init");
            }
        },
        users: {
            init: function () {
                console.log("users.init");
            },
            show: function () {
                 console.log("users.show");
            }
        }
    }
})();

PATTERN #2 (singleton)

var MODULE = {
  common: {
    init: function() {
        console.log("common.init");
    }
  },

  users: {
    init: function() {
      console.log("users.init");
    },

    show: function() {
      console.log("users.show");
    }
  }
};
  • 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-25T22:26:47+00:00Added an answer on May 25, 2026 at 10:26 pm

    Personally, I recommend an extension of #1, as follows:

    var Module = (function(Module) {
      // A comment
      Module.variable1 = 3;
    
      /**
       * init()
       */
      Module.init = function() {
        console.log("init");
      };
    
      // ...
    
      return Module;
    })(Module || {});
    

    I like this pattern for a couple reasons. One, documentation (specifically javadoc-style) look more natural when all your functions are declarations rather than a big hash. Two, if your submodules grow in size, it lets you break them into multiple files without any refactoring.

    For example, if Module.Users were to go into its own file:

    var Module = Module || {};
    Module.Users = (function(Users) {
      /**
       * init()
       */
      Users.init = function() {
        console.log("Module.Users.init");
      };
    
      // ...
    
      return Users;
    })(Module.Users || {});
    

    Now “module.js” and “module.users.js” can be separate files, and they’ll work regardless of the order they are loaded. Also note the local scoping of the module name – this is very handy if your module name is long, because you can take “MyApp.Users.EditScreen” and refer to it with a variable like “ES” within the scope of your module definition.

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

Sidebar

Related Questions

I am trying to organize a Zip Archive into an array so that I
I am trying to organize my application into packages because it will have several
I am trying to organize my program into functions and have ran into this,
I'm trying to organize my php-project by puting some files into folders. When I
I have Python nested list that I'm trying to organize and eventually count number
I'm trying to work out how to organize an android application that will have
Trying to make a make generic select control that I can dynamically add elements
I am trying to make a table inside a table so I can organize
I am trying to organize my JavaScript. What I would like to do is
I'm trying to figure out how to best manage Javascript file dependencies and have

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.