Explain the following javascript statement? -
this question has answer here:
- how javascript closures work? 89 answers
- why need invoke anonymous function on same line? 18 answers
var ninja = (function(){ function ninja(){} return new ninja(); })();
i've been learning javascript while now. why function declaration above encapsulated in '('s , why there '();' in end?
i can imagine constructor function, because of '();' in end. why object wrapped in simple brackets?
this code equivalent to:
function ninja() { // nothing here } var ninja = new ninja();
though in code listed, function/object ninja not global scope.
the code (function() {...})();
says "take whatever function contained inside here , execute immediately". it's creating anonymous function , calling right afterwards.
Comments
Post a Comment