Taken from wikipedia, since it's well explained.This is a immediately-invoked function expression
(function(){
/* code */
}());
Taken from wikipedia, since it's well explained.
An immediately-invoked function expression (or IIFE, pronounced "iffy") is a JavaScript design pattern which produces a lexical scope using JavaScript's function scoping. Immediately-invoked function expressions can be used to avoid variable hoisting from within blocks, protect against polluting the global environment and simultaneously allow public access to methods while retaining privacy for variables defined within the function.
See here for a more detailed explanation.
I get that it creates an anonymous function and then executes it. But, can't figure out why it has to be that way. Isn't that equivalent to a more readable form:
function initSomething() {}
initSomething();
It's not the same since initSomething can be still referenced and thus is not anonymous.