I came across the following pattern recently:
/* PATTERN 1 */
(function(window) {
var Stuff = (function() { // Variable assignment and self-invoking anonymous function
function Stuff(params) {
this.items = [];
}
Stuff.prototype = {
someMethod1: function() {
},
someMethod2: function() {
}
};
return Stuff;
}()); // END Variable assignment
Stuff.create = function(params) {
return new Stuff(params);
};
window.Stuff = Stuff;
}(window));
What confuses me is the role that assigning the Stuff
variable plays. Specifically, how is this pattern operationally different to the following:
/* PATTERN 2 */
(function(window) {
// No variable assignment or self-invoking anonymous function
function Stuff(params) {
this.items = [];
}
Stuff.prototype = {
someMethod1: function() {
},
someMethod2: function() {
}
};
Stuff.create = function(params) {
return new Stuff(params);
};
window.Stuff = Stuff;
}(window));
Is the scope of pattern 1's prototype methods private in some way that pattern 2's prototype methods aren't? Is this purely a stylistic approach for more clearly separating business logic?