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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:35:53+00:00 2026-06-13T06:35:53+00:00

Possible Duplicate: What advantages does using (function(window, document, undefined) { … })(window, document) confer?

  • 0

Possible Duplicate:
What advantages does using (function(window, document, undefined) { … })(window, document) confer?

I’m increasingly seeing code like this in libraries I’ve been using:

(function (window) {
    var Foo = function () {
    }

    window.Foo = Foo;
})(window);

The argument I’ve seen for doing this is to avoid working in the global scope when creating (pseudo) classes. But, correct me if I’m wrong, I always understood that window IS the global scope. I believe, when you create a global variable, you are really only adding a property to window anyway… Unless this is changing for ES5 strict?

So, basically, what’s the point? The only benefit I can see to code organised like this is if you wanted to easily change the namespace of your classes at a later date by passing in an argument other than window.

  • 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-13T06:35:53+00:00Added an answer on June 13, 2026 at 6:35 am

    Infact, strict mode throws an exception if you forget to use var for any variable declaration. But that works even without using an outer closure.

    Using this pattern is much more for protecting yourself from the outside javascript world. For instance, some other script overwrites window.undefined or any other variable, you can grab the value within that closure to savely access it from within.

    For instance

    (function _myApp( win, doc, undef ) {
        // app code
    }( this, this.document ));
    

    Also, when declaring variables with var, or creating function declarations, those are always stored within the current Activation Object respectively the Lexical Environment Record. That means, without using a Function context, you can easily overwrite methods and variables from some other point, because all of those would get stored in the current Context (which would be the global one)

    So:

    (function _myApp( win, doc, undef ) {
        var myVar = 42;
    
        function myFunc() {
        }
    }( this, this.document ));
    
    (function _myModule( win, doc, undef ) {
        var myVar = 42;
    
        function myFunc() {
        }
    }( this, this.document ));
    

    This works because of closure and Context, but if you would use the same code, without the Function Context, we would obviously overwrite our myVar and myFunc. This could happen from everywhere, within the same file or in another loaded script.

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

Sidebar

Related Questions

Possible Duplicate: new MyObject(); vs new MyObject; I keep seeing code like this one:
Possible Duplicates: How does this JavaScript/JQuery Syntax work: (function( window, undefined ) { })(window)?
Possible Duplicate: Use of ‘prototype’ vs. ‘this’ in Javascript? Advantages of using prototype, vs
Possible Duplicate: What's the point of the var keyword? What advantages does using var
Possible Duplicate: Javascript - this Vs. prototype Advantages of using prototype, vs defining methods
Possible Duplicate: Why does this C code work? How do you use offsetof() on
Possible Duplicate: How can I understand nested ?: operators in PHP? Why does this:
Possible Duplicate: Can main function call itself in C++? I found this problem very
Possible Duplicate: In C++ why can’t I write a for() loop like this: for(
Possible Duplicate: Advantages of using prototype, vs defining methods straight in the constructor? What

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.