2

I started JavaScript programming last month, i know basic syntax like objects, functions, class, string, array, property. Now i was puzzled on given syntax.

Syntax:

(function(){

 "use strict";

})();

My question is, why two parenthesis( one contains 'function' and another "empty" ) are included in the definition? can some one help me why we use this?.

Pointy
  • 405,095
  • 59
  • 585
  • 614
Ashif
  • 1,652
  • 14
  • 30

4 Answers4

2

That will create an anonymous function and then immediately execute it.

In this case that function has a single line..."use strict".

Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
2

That code doesn't have any net impact on the world. What happens is:

  • The parenthesized function is instantiated;
  • The final () cause that function to be called;
  • The "use strict" constant turns on "strict" mode;
  • The function returns.
Pointy
  • 405,095
  • 59
  • 585
  • 614
1

This is a self-invoked anonymous function (or immediately invoked function expression, also known as IIFE), a pattern that in this case exists just to produce a closed scope.

Apparently, they don't want "use strict"; turning on strict mode for the whole file, or when minified and concatenated to other files, turning on strict mode on included libraries, etc. Amazon had a problem with this a while back. Since then, it's been recommended best practice to keep "use strict" in function scope, and this function exists solely for that purpose.

Plynx
  • 11,341
  • 3
  • 32
  • 33
  • I don't mean to pick on you but a popular alternative term is "immediately-invoked function expression" (IIFE), which is a bit more accurate since the function doesn't really invoke itself. – Pointy Feb 07 '13 at 23:32
  • @Pointy I actually googled both expressions before writing the answer to see which would lead to more useful results, but you're right. – Plynx Feb 07 '13 at 23:34
  • I called them that for quite a while before somebody asked me how exactly the function managed to execute itself :-) – Pointy Feb 07 '13 at 23:35
  • @Pointy :O YOU'RE RIGHT! – Plynx Feb 07 '13 at 23:39
1

What you have there is a self-invoking function.

It pretty much is exactly that, a function which immediately gets called. To accomplish that, we need a function expression, not a function declaration. To achieve that, we can do certain things, one of those is, to put the whole expression in parenthesis

(function() {});

this statement creates a function expression. Now all we have left to do is, to invoke that function by appending additional function parenthesis, like we would do with any function

(function() {})();

You can also put the function parenthesis into the whole statement, it makes no difference

(function() {}());

Another option to bring a function into an expression form is by using ! or + signs infront of it

!function(){}()

Anything goes, as long we create an expression, we can't invoke a function declaration like that

function foo(){}() // syntax error
jAndy
  • 231,737
  • 57
  • 305
  • 359