javascript - How do these patterns differ? -
how this:
var obj = obj || {}; obj.something = (function() { function something() { }; something.prototype.somemethod = function() { }; return something; })();
differ this:
obj.something = function() { }; obj.something.prototype = { };
how patterns differ? when use 1 on other?
the main reason why use way teacher recommends may define other variables , functions wouldn't exposed in global scope.
for example :
obj.something = (function() { var sum = 0; function something() { }; something.increment = function(){ sum++ }; something.getsum = function(){ return sum }; return something; })();
here sum
isn't public , doesn't pollute global namespace.
other that, both patterns pretty similar. it's idea accustomed iife pattern when, do, try know why it's useful , not apply it.
Comments
Post a Comment