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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T13:13:44+00:00 2026-06-11T13:13:44+00:00

I was reading through the javascript design patterns book and came across the code

  • 0

I was reading through the javascript design patterns book and came across the code below, while reading “The Command Pattern”. I’ve been trying to understand why this code is wrapped around an anonymous function which is immediately invoked, especially since there is no other private variables to be closured with. How is this different from just declaring CarManager as an object literal?

(function(){

  var CarManager = {

    // request information
    requestInfo: function( model, id ){
      return 'The information for ' + model + ' with ID ' + id + ' is foobar';
    },

    // purchase the car
    buyVehicle: function( model, id ){
      return 'You have successfully purchased Item ' + id + ', a ' + model;
    },

    // arrange a viewing
    arrangeViewing: function( model, id ){
      return 'You have successfully booked a viewing of ' + model + ' ( ' + id + ' ) ';
    }

  };

})();
  • 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-11T13:13:45+00:00Added an answer on June 11, 2026 at 1:13 pm

    The example is slightly half-baked.

    The goal is to be able to provide a public-interface.
    Inside of that IIFE, you put together all of the pieces required for your interface.

    You don’t have to pass it back out through a return statement.

    jQuery, for example, is built in the same way.
    Instead of having a return statement:

    var jQuery = (function () { return magic; }());
    

    They manually set window‘s jQuery (and $) properties from within the function, like so:

    (function () { window["jQuery"] = magic; }());
    

    Moreover, what’s actually happening is a little bit different, still.
    They’re passing the window object into the IIFE’s window parameter.
    It doesn’t have to be window, then.
    It could be any previously-defined object you’re extending, in theory.

    var myApp = {};
    (function (window, document) { window["jQuery"] = public_interface; }(myApp, document));
    myApp.jQuery("...");
    

    When building libraries similar to jQuery, you can do similar.
    If you typically namespace any large libraries/applications (a good idea), you can shorten the name, internally, to make life easier.

    Instead of:

    var NAMESPACED_AWESOME_APP_OF_DOOM = {};
    NAMESPACED_AWESOME_APP_OF_DOOM.module1 = {};

    You can do something like:

    (function (namespace) {
    
        var ns = {};
        window[namespace] = ns;
    
        ns.module1 = {};
        ns.module2 = {};
        ns.service1 = function () {};
    
    }("NAMESPACED_AWESOME_APP_OF_DOOM"));
    

    Then you can do all of your internal setup inside, and once your setup is complete, you can reference the application by name, in the global scope.

    In other cases, you can create sandboxes for code, using closures in this way, with mediators to send messages back and forth between components.
    Nicholas Zakas has a couple of good talks on this.

    And lastly, sometimes you just want some work to get done, which is 100% irrelevant to the rest of your program, but still needs to get done, all the same (like doing compatibility-checks and tying the results to the DOM elements, a la Modernizr… …or setting cookies that just need to be there, or firing off calls to analytics software… …et cetera).

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

Sidebar

Related Questions

While reading through javascript codes I've been seeing the ! operator used for non
I've been trying to wrap my head around javascript prototypal inheritance and while reading
I've been reading up on Mozilla's continuing development of JavaScript, up through version 1.8.5:
So I've been reading through Javascript - The Good Parts and one thing that
I m reading John Resig s Learning Advanced JavaScript http://ejohn.org/apps/learn/#10 and came across this
I was reading through my javascript file, and two questions came to my mind:
I've gone through Javascript: The Good Parts and currently reading Javascript Patterns, but would
I was reading through this thread: Hidden Features of JavaScript? and found this post:
While reading through another question here, on creating a URL shortening service, it was
I was reading through this interesting post about using JavaScript to generate an image

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.