Skip to content Skip to sidebar Skip to footer

Strange Javascript Syntax Like This: (function(){//code}) ();?

What does the below JavaScript mean? Why is the function embedded inside ()? (function() { var b = 3; a += b; }) ();

Solution 1:

It's functionaly equivalent to doing something like:

var myFunc = function(){
    var b = 3;
    a += b;
};

myFunc();

It's got the parenthesis around it (and trailing) so that the function is called immediately. As others have said, the concept is called an anonymous function.

Solution 2:

This is an anonymous functions, which fires just after its creation.

Solution 3:

Its an anonymous function.

Anonymous functions are functions that are dynamically declared at runtime that don’t have to be given a name.

Anonymous functions are declared using the function operator. You can use the function operator to create a new function wherever it’s valid to put an expression. For example you could declare a new function as a parameter to a function call or to assign a property of another object.

For further reading

Javascript anonymous functions

Anonymous functions can help make code more concise when declaring a function that will only be used in one place. Rather than having to declare the function and then use it you can do both in one step. It’s particularly useful for things like declaring event handlers and assigning methods to objects.

For example, if we’re creating a constructor function, we’ll need to declare the object’s methods and then assign them to the object’s properties so they can be called outside the object. It’s possible to declare the function and then assign it to a variable as a separate step like this:

function Pet(name, species, hello)
{    
          this.name = name;    
          this.species = species;    
          this.hello = hello;

          function sayHello()    
          {    
              alert(this.hello);    
          }

          this.sayHello = sayHello;    
      }

But it’s a bit more convenient and concise to do it all as one step:

 function Pet2(name, species, hello)
 {
      this.name = name;    
      this.species = species;    
      this.hello = hello;

      this.sayHello = function()      
      {     
          alert(this.hello);    
      }    
  }

Solution 4:

What you wrote is an anonymous function called immediately. The reason for doing that is use of private variables. If instead of your code there would be:

var b = 3;
a += b;

b will be global variable. So if you need in global code private variable, that is the way to do this.

Solution 5:

It's an anonymous function, that is called directly after creation, then thrown away.

It's inside a parenthesis to prevent a syntax error. Without the parentheses the keyword function needs to be followed by an identifier.

You can also do like this to put the function in a variable, and then call it:

var x = function() {
   var b = 3;
   a += b;
}
x();

Notice that the parentheses are not needed when the function keyword is not first.

Post a Comment for "Strange Javascript Syntax Like This: (function(){//code}) ();?"