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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T16:08:21+00:00 2026-06-06T16:08:21+00:00

I’m using the javascript module pattern, and have this so far: var APP; if(APP

  • 0

I’m using the javascript module pattern, and have this so far:

var APP;
if(APP == undefined) {
  APP = {};
}

APP = (function() {
    var userId = -1;
    var privateVar = '';

    var _init = function($userId) {
       userId = $userId;
    };
    var _publicMethod = function($id){
        privateVar = id;
    };
    return {
        init = function($userId) {
             _init($userId);
         },
        publicMethod = function($id) {
           _publicMethod($id);
        }
    };
})();

I then have a common utils module:

APP.utils = (function(){

})();

And then per page I am planning on having a module, so I don’t wireup events (button clicks etc) for no reason on pages where these DOM elements don’t event exist:

APP.homePage = (function(){
     return {

     };
})();

So each module will have a init() method that I will call on the page to run things that have to be run (e.g. wiring up events, setting variables like say userId etc):

$(document).ready(function() {
   APP.init(<%= user.id %>);
   APP.homePage.init('abc');
});

So now if the files get too big, I can break them out into separate files also.

  1. What if one module needs to call another, I guess the only way for this to work is to do this via the public api right?
    e.g. what if homePage needs userId, should I pass that in the homePage#init method?

  2. How is my coding style, any obvious style that is not considered best practise?

  3. Any comments on this approach? Is it good in general?
  • 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-06T16:08:23+00:00Added an answer on June 6, 2026 at 4:08 pm

    What if one module needs to call another, I guess the only way for this to work is to do this via the public api right?

    Yes

    e.g. what if homePage needs userId, should I pass that in the homePage#init method?

    No. I’d not repeat the userId code in all modules, but offer a public getter for it in the default module.

    Any comments on coding

    This code

    var APP;
    if(APP == undefined) {
      APP = {};
    }
    
    APP = ...
    

    is quite useless. You don’t need to check for object existance here, because you overwrite it anyway. That also means that this code must be the first to execute. If you want to make the modules independent from load order, you’d need to use something like

    var APP = (function(a) {
        var private_vars; // ...
        a.init = ...
        a.publicMethod = ... // add them to the object instead of creating new one
        a.getPrivate = function() {
            return private_vars;
        };
        return a;
    })(APP || {}); // create one iff not already existing
    
    // other file:
    var APP = APP || {};
    APP.utils = ... // add object to namespace
    

    The code

    var _publicMethod = function($id){
        privateVar = id;
    };
    

    looks a bit odd. First, the underscore usually denotes a semiprivate (public-but-not-to-be-used) attribute of objects and should not be used for variable names. That’s not the case in here as the function will be exposed as the “publicmethod” property of APP. Use the underscore there if you want it. Second, there is no need to use a function expression here. The code is in the module’s local scope, and using a function declaration both makes it available everywhere in that scope and allows naming the function. You should use

    function publicMethod($id) {
        privateVar = id;
    }
    a.publicMethod = publicMethod;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I am reading a book about Javascript and jQuery and using one of the
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I have thousands of HTML files to process using Groovy/Java and I need to

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.