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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T02:09:50+00:00 2026-05-20T02:09:50+00:00

I guess using this pattern is the new hotness, but I don’t understand what

  • 0

I guess using this pattern is the new hotness, but I don’t understand what the advantage is and I don’t understand the scoping implications.

The pattern:

(function(window, document, undefined){
  window.MyObject = {
    methodA: function() { ... },
    methodB: function() { ... }
  };
})(window, document)

So I have several questions about this.

Is there a particular advantage to encapsulating an object like this?
Why are window and document being fed in instead of just being accessed normally?
Why the heck is undefined being passed in?
Is attaching the object we’re creating directly to window a particularly good idea?

I’m used to what I’ll call the Crockford style of Javascript encapsulation (because I got it off the Douglas Crockford Javascript videos).

NameSpace.MyObject = function() {
  // Private methods
  // These methods are available in the closure
  // but are not exposed outside the object we'll be returning.
  var methodA = function() { ... };

  // Public methods
  // We return an object that uses our private functions,
  // but only exposes the interface we want to be available.
  return {

    methodB: function() {
      var a = methodA();
    },
    methodC: function() { ... }

  }
// Note that we're executing the function here.
}();

Is one of these patterns functionally better than the other? Is the first one an evolution of the other?

  • 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-20T02:09:51+00:00Added an answer on May 20, 2026 at 2:09 am

    Why are window and document being fed in instead of just being accessed normally?

    Generally to fasten the identifier resolution process, having them as local variables can help (although IMO the performance improvements may be negligible).

    Passing the global object is also a widely used technique on non-browser environments, where you don’t have a window identifier at the global scope, e.g.:

    (function (global) {
      //..
    })(this); // this on the global execution context is 
              // the global object itself
    

    Why the heck is undefined being passed in?

    This is made because the undefined global property in ECMAScript 3, is mutable, meaning that someone could change its value affecting your code, for example:

    undefined = true; // mutable
    (function (undefined) {
      alert(typeof undefined); // "undefined", the local identifier
    })(); // <-- no value passed, undefined by default
    

    If you look carefully undefined is actually not being passed (there’s no argument on the function call), that’s one of the reliable ways to get the undefined value, without using the property window.undefined.

    The name undefined in JavaScript doesn’t mean anything special, is not a keyword like true, false, etc…, it’s just an identifier.

    Just for the record, in ECMAScript 5, this property was made non-writable…

    Is attaching the object we’re creating directly to window a particularly good idea?

    It’s a common way used to declare global properties when you are on another function scope.

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

Sidebar

Related Questions

I guess the topic says it. I have tried googling this, but havent gotten
I am using the MVVM pattern, I have a view which creates a new
I'm using CakePHP to build an application using the MVC Pattern, but my question
I guess I have been using the Web Site model ever since .NET 2.0
If you are working in PHP (or I guess any programming language) and using
I guess this is an odd one, and the answer is most likely it
I guess the real question is: If I don't care about dirty reads, will
I guess I'll illustrate with an example: In this game you are able to
I'm getting a java outOfMemoryError when I call this method - i'm using it
These days, I've seen a URL pattern like this: http://stackoverflow.com/questions/How-to-do Not a usual and

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.