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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:09:59+00:00 2026-05-13T22:09:59+00:00

I have been reading a lot of Javascript lately and I have been noticing

  • 0

I have been reading a lot of Javascript lately and I have been noticing that the whole file is wrapped like the following in the .js files to be imported.

(function() {
    ... 
    code
    ...
})();

What is the reason for doing this rather than a simple set of constructor functions?

  • 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-13T22:10:00+00:00Added an answer on May 13, 2026 at 10:10 pm

    It’s usually to namespace (see later) and control the visibility of member functions and/or variables. Think of it like an object definition. The technical name for it is an Immediately Invoked Function Expression (IIFE). jQuery plugins are usually written like this.

    In Javascript, you can nest functions. So, the following is legal:

    function outerFunction() {
       function innerFunction() {
          // code
       }
    }
    

    Now you can call outerFunction(), but the visiblity of innerFunction() is limited to the scope of outerFunction(), meaning it is private to outerFunction(). It basically follows the same principle as variables in Javascript:

    var globalVariable;
    
    function someFunction() {
       var localVariable;
    }
    

    Correspondingly:

    function globalFunction() {
    
       var localFunction1 = function() {
           //I'm anonymous! But localFunction1 is a reference to me!
       };
    
       function localFunction2() {
          //I'm named!
       }
    }
    

    In the above scenario, you can call globalFunction() from anywhere, but you cannot call localFunction1 or localFunction2.

    What you’re doing when you write (function() { ... })(), is you’re making the code inside the first set of parentheses a function literal (meaning the whole “object” is actually a function). After that, you’re self-invoking the function (the final ()) that you just defined. So the major advantage of this as I mentioned before, is that you can have private methods/functions and properties:

    (function() {
       var private_var;
    
       function private_function() {
         //code
       }
    })();
    

    In the first example, you would explicitly invoke globalFunction by name to run it. That is, you would just do globalFunction() to run it. But in the above example, you’re not just defining a function; you’re defining and invoking it in one go. This means that when the your JavaScript file is loaded, it is immediately executed. Of course, you could do:

    function globalFunction() {
        // code
    }
    globalFunction();
    

    The behavior would largely be the same except for one significant difference: you avoid polluting the global scope when you use an IIFE (as a consequence it also means that you cannot invoke the function multiple times since it doesn’t have a name, but since this function is only meant to be executed once it really isn’t an issue).

    The neat thing with IIFEs is that you can also define things inside and only expose the parts that you want to the outside world so (an example of namespacing so you can basically create your own library/plugin):

    var myPlugin = (function() {
     var private_var;
    
     function private_function() {
     }
    
     return {
        public_function1: function() {
        },
        public_function2: function() {
        }
     }
    })()
    

    Now you can call myPlugin.public_function1(), but you cannot access private_function()! So pretty similar to a class definition. To understand this better, I recommend the following links for some further reading:

    • Namespacing your Javascript
    • Private members in Javascript (by Douglas Crockford)

    EDIT

    I forgot to mention. In that final (), you can pass anything you want inside. For example, when you create jQuery plugins, you pass in jQuery or $ like so:

    (function(jQ) { ... code ... })(jQuery) 
    

    So what you’re doing here is defining a function that takes in one parameter (called jQ, a local variable, and known only to that function). Then you’re self-invoking the function and passing in a parameter (also called jQuery, but this one is from the outside world and a reference to the actual jQuery itself). There is no pressing need to do this, but there are some advantages:

    • You can redefine a global parameter and give it a name that makes sense in the local scope.
    • There is a slight performance advantage since it is faster to look things up in the local scope instead of having to walk up the scope chain into the global scope.
    • There are benefits for compression (minification).

    Earlier I described how these functions run automatically at startup, but if they run automatically who is passing in the arguments? This technique assumes that all the parameters you need are already defined as global variables. So if jQuery wasn’t already defined as a global variable this example would not work. As you might guess, one things jquery.js does during its initialization is define a ‘jQuery’ global variable, as well as its more famous ‘$’ global variable, which allows this code to work after jQuery has been included.

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

Sidebar

Related Questions

I have been reading a lot about Haskell lately, and the benefits that it
I have been reading a lot on design patterns lately and some of them
Lately I have been reading a lot of blog topics about big sites(facebook, twitter,
I have been reading a lot about test-driven development and decided that I want
I have been reading to stay away from JavaScript for a lot of things,
I have been reading a lot about this - I feel like I'm very
I have been reading a lot about Reinforcement Learning lately, and I have found
I have been reading a lot of articles lately about programming practice, design and
I have been reading about Factory pattern a lot lately. I am trying to
I have been lately reading a lot on memory allocation schemes in java, 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.