Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What means construction (function () { ... } ) () [duplicate]

Tags:

javascript

What means this construction in JavaScript :

   (function (){
      alert("bla");
   })();

?

like image 994
Oto Shavadze Avatar asked Sep 17 '26 05:09

Oto Shavadze


1 Answers

The acronym for this pattern is an "IIFE" or an Immediately Invoked Function Expression.

It basically creates an anonymous function function(){}

function(){alert("bla");}

then wraps it as an expression ()

(function(){alert("bla");})

then executes it ()

(function(){alert("bla");})()

Note that at this point, you can pass arguments in as well like this:

(function(text){alert(text);})("bla")
like image 67
Travis J Avatar answered Sep 19 '26 17:09

Travis J



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!