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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T23:04:30+00:00 2026-06-02T23:04:30+00:00

I understand the concept of variable scope in the following example, but can someone

  • 0

I understand the concept of variable scope in the following example, but can someone explain the function-wrapping syntax (...)();, e.g. how do you use it in actually day-to-day JavaScript programming? It’s not something that I know from PHP/Java/C#.

window.onload = function() {
    var i = 4;
    console.log(i); //4
    (function showIt() {
        var i = 'whatever';
        console.log(i); //whatever
    })();
    console.log(i); //4
};
  • 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-02T23:04:32+00:00Added an answer on June 2, 2026 at 11:04 pm

    There are several ways in which this form is useful. One is to lexically scope a segment of code so that its inner variables and methods stay separate from the larger body of code that contains it. In this way, it’s JavaScript’s way of doing block scoping. But the most common way I use this format is as an alternative to this:

    var ret = {
        depth:0,
        initialized:false,
        helper:function() { /*help with some stuff*/ },
        initialize:function(){ /*do some initialization code*/ },
        action:function(){/*do the thing you want*/}
        destroy:function() { /*clean up*/ }
    }
    

    The thing that absolutely kills me about this format is it is extremely time consuming to find missing braces and commas. For example, the code above won’t work because the’s no comma at the end of the action declaration and unless I had pointed it out, you’d have had a hard time finding the problem because when the exception is thrown, it’s thrown on the entire statement, not the section that’s “causing the problem”. This is such a predictable problem that I simply don’t use this format any more if I can possibly avoid it. I refuse. Instead, the same can be written much more clearly as:

    var ret = (function(){
        var self = {},
            initialized = false;
    
        var helper = function() { /*help with some stuff*/ };
    
        self.depth = 0;
        self.initialize = function() {/*do some initialization*/};
        self.action = function() {/*do the thing you want*/};
        self.destroy = function() { /*clean up*/ };
    
        return self;
    }());
    

    There are two big advantages for me. One, missing braces and commas can be found more easily (when the exception is thrown, the line number will be close to the area where it’s missing). And two, you can choose to keep some variables and methods private and you retain all the benefits of the first block of code.

    And the last plug I’ll give for this format is that the code above (which is sort of like a Singleton) can be converted into a constructor by 1) removing the invocation braces on the outside, 2) changing self = {} to self = this, and 3) optionally removing the return self at the end:

    var Ret = function(){
        var self = this,
            initialized = false;
    
        var helper = function() { /*help with some stuff*/ };
    
        self.depth = 0;
        self.initialize = function() {/*do some initialization*/};
        self.action = function() {/*do the thing you want*/};
        self.destroy = function() { /*clean up*/ };
    
        return self; // this is ignored by the compiler if used as a constructor
    };
    var ret = new Ret();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can someone explain me the visibility of the session variable in Rails. I mean
I understand the concept and reasons behind using the using statement, and I use
I completely understand the concept of expression trees, but I am having a hard
I can't understand the concept and, first of all, where it belongs. Is it
I am trying to understand the concept of currying and calling a function which
What's the concept behind zip compression? I can understand the concept of removing empty
Possible Duplicate: What's an actual use of variable variables? i saw the concept of
I don't understand the concept of the fetch function. I am doing a tutorial
I understand the theoretical concept that assigning one reference type variable to another, only
I asked this function based on this concept (maybe incorrect?!): Wherever a const can

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.