The template
(function(){})();
Is a template of an anonymous function.
To explain it, let's create a function x()
that alerts 'hello':
function x() {
alert('hello');
}
To call x
, we will:
x();
Now, it's clear that if we replace an object's name with it's value, the statememt will stay the same (take for example var k = 5; alert(k); alert(5)
- the two alerts are the same because variable's name was replaced with it's value).
In our case, x
's value is:
function () {
alert('hello');
}
So if we replace x
with it's value in the statement x();
->
(function () {
alert('hello');
})();
And that's the source of the syntax. The result of that will be an immediate call to the specified function. Of course, this syntax works also for functions with parameters and functions with return types.