Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different ways of creating functions in javascript

First of all - yes, I know there are plenty of posts regarding this, or at least very similar ones. Having looked through those I still haven't found the answer to what I'm looking for:

I've learned that there are two main ways to create functions in javascript:

var test = function(a){
   console.log(a);
}

Which is created at runtime, and:

function test(a){
    console.log(a);
}

Which is created before runtime.

Today I saw this one:

(function test(a){
    console.log(a);
})();

I have never seen that before. What separates this one from the two above?

like image 234
Nilzone- Avatar asked Nov 05 '13 13:11

Nilzone-


1 Answers

This is immediately-invoke function, it will call itself immediately after declaration.

You can read more about Immediately-invoked function expression at wikipedia.

like image 167
antyrat Avatar answered Sep 30 '22 16:09

antyrat