Possible Duplicate:
What do empty parentheses () after a function declaration do in javascript?
I understand basically how Javascript works. Now I'm self-learning Javascript design patterns by going through other programmers' works and I come across this
var $a = (function() {
var a..... //assigning values & functions to variables
return { init : init }; //in the variable above there is "init"
})();
$a.init();
I can tell that $a.init()
creates an object that has the properties & functions listed above. But I don't understand how it actually work.
Why is the function written this way(function() { })()
?
Why is return {init: init}
necessary when there is already an init
function above?
What kind of pattern is this?