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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T04:07:59+00:00 2026-05-30T04:07:59+00:00

These two JavaScript patterns are very similar. I would like to know which one

  • 0

These two JavaScript patterns are very similar. I would like to know which one is better and why and how either one can be improved.

First Approach:

"MODULE" in window || (window.MODULE = {} );

MODULE.utils = (function ($) {

    var utils = {};

    //public
    utils.todo = function() {
        //#
    }

    //private
    function init() {
        //#
    }

    init();

    return utils;
}(jQuery));

Second Approach:

"MODULE" in window || (window.MODULE = {} );

MODULE.utils = (function() {

    function todo(){
        //#
    }

    function init() {
        //#
    }

    return {
        init:init
    }

})();


$(function() {
    MODULE.utils.init();
});
  • 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-30T04:08:01+00:00Added an answer on May 30, 2026 at 4:08 am

    Both of your options don’t really have much in the way of pros or cons, it’s more just about personal preference. Both could be adjusted to provide scope a bit better.

    I have my own preference, it relies on Underscore. It doesn’t really promote private variables or functions but I rarely find this an issue. If you want to introduce jQuery, etc, it would be best to wrap in an anonymous function to $ is actually jQuery (or an interchangeable library).

    As you’ll see below, my preference requires a little more code to get you going (although some of it’s not necessary), but having tried a few variations of what you originally proposed, I’ve found that my solutions lends itself to more understandable code and it’s easier for other devs to get the grasp of what’s going on, especially if they’ve got experience with Backbone.View.

    EDIT: Wrapped in an anonymous function to demonstrate integrating jQuery and protected scope.

    var MyNamespace = MyNamespace || {};
    
    (function($, MyNamespace) {
    
        MyNamespace.MyModule = function(options) {
            this.defaults = this.defaults || {};
            // have had trouble with _.defaults so _.extend instead
            this.options = _.extend({}, this.defaults, options);
            this.initialize.call(this);
            // define private stuff in here if you want
        };
    
        _.extend(MyNamespace.MyModule.prototype, {
    
            defaults: {
                myOption: "test"
            },
    
            initialize: function()
            {
                // ensure this always refers to our MyModule instance
                _.bindAll(this);
                this.$el = $("#some-widget");
                // Look Ma! log is already binded to this!
                this.$el.on("click", this.log);
            },
    
            setMyOption: function(value)
            {
                this.options.myOption = value;
            },
    
            log: function()
            {
                console.log("myOption: ", this.options.myOption);
            }
    
        });
    
    })(jQuery, MyNamespace) 
    
    var myModule = new MyNamespace.MyModule({ myOption: "Hey SO!" });
    myModule.log(); // -> myOption: Hey SO!
    myModule.setMyOption("Setter");
    myModule.log(); // -> myOption: Setter
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to know how to merge these two Javascript ajax scripts. So
Can anyone confirm that these samples from Chapter 3 of Pro Javascript Design Patterns
As far as I can tell, these two pieces of javascript behave the same
Can anybody tell me the difference between these two JavaScript commands? Also what is
What are the differences between these two codes in JavaScript? var obj = new
In javascript, is there any different between these two: // call MyFunction normal way
These two may look like they have no correlation but bear with me! In
I have two JavaScript code snippets. These perform specific task when an 'Update' button
I have below a javascript function which gets called on click of two separate
What is the difference between these two javascript function calls? (function(){alert(foo)})() versus this: (function(){alert(foo)}())

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.