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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:05:52+00:00 2026-05-27T23:05:52+00:00

Possible Duplicate: javascript function vs. ( function() { … } ()); Sorry if this

  • 0

Possible Duplicate:
javascript function vs. ( function() { … } ());

Sorry if this is too basic, but what is this construct do?

(function MyFunction() {
  /* Some codes ... */    
})();

Maybe there is a special term for that? It can be useful for Googling, instead of just putting that snippet in the search field.

  • 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-27T23:05:52+00:00Added an answer on May 27, 2026 at 11:05 pm

    It’s called the direct invocation pattern. It defines an anonymous function, and then immediately executes it. This is useful for ‘private’ variables and such. If you’d normally do this:

    // global namespace
    var data = 'lol';
    function getData() {
      return data;
    }
    

    there’d be a variable data in the global namespace and if someone entered data = 123 in a web console it could break your script. Using the direct invocation pattern, you can do this:

    // global namespace
    /*lotsofcodehere*/
    (function MyFunction() {
      // closure namespace
      var data = 'lol';
      this.getData = function getData() {
        return data;
      }
    })();
    // global again
    

    In this case, the function getData will still exist in the global namespace, but data will be inaccessible from outside the closure namespace.

    You’ll also notice MyFunction won’t exist in the global namespace when using this pattern. That is because of one of many small language rules, but basically a function is always available by it’s name inside the function. If you have something like this:

    // returns the amount of from--s needed to get to 0 :D
    // (yes, it returns it's input number :D)
    (function() {
    var i = 0, keep = false;
    
    this.countToZero = function(from) {
      if(from === 0) {
        keep = false; // don't keep i's contents on next invocation
        return i;
      }
      if(!keep) i = 0; // reset i on first invocation
      i++;
      keep = true;
      return countToZero(from - 1);
    }
    })();
    

    It works perfectly, and countToZero(5) will nicely return 5. But well, it’s not really nice if you use it in the non-global namespace, if this is used inside a function it’ll define countToZero as a member property of that function, which will break our return (as countToZero is no longer accessible through the global namespace)
    This is not a realistic scenario perhaps, but it works for this example. Instead of the above code, we’ll use this:

    /*code*/
    this.countToZero = function countToZero(from) {
      // countToZero will *always* exist here as a reference to this function!
      /*code*/
      return countToZero(from);
    };
    

    This code is quite hard to break, except if you pass Infinity as the first param of course, even if you use it in completely ridiculous ways.

    …

    did I say I was going to provide clear explanation or nice, real-life examples? I hope I didn’t

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

Sidebar

Related Questions

Possible Duplicate: What does this mean? (function (x,y)){…}){a,b); in JavaScript I have the following
Possible Duplicate: What does this mean? (function (x,y)){…}){a,b); in JavaScript Please anyone explain and
Possible Duplicate: What does this “(function(){});”, a function inside brackets, mean in javascript? javascript
Possible Duplicate: Is JavaScript’s math broken? This seems really stupid, but when running this,
Possible Duplicate: JavaScript function aliasing doesn't seem to work Why doesn't this work? function
Possible Duplicate: Javascript function to convert color names to hex codes Is it possible
Possible Duplicate JavaScript: Changing src-attribute of a embed-tag but this is not working for
Possible Duplicates: What does this mean? (function (x,y)){…}){a,b); in JavaScript What do parentheses surrounding
Possible Duplicate: JavaScript scope and closure What is this for? (function(){ //The code to
Possible Duplicate: JavaScript Function Definition in ASP User Control Hi, I have a generic

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.