I have been in constant search for the most simple Javascript module pattern. I have read pretty much every article I can find. Most module patterns like to create an object, add functions to it, then return that object and expose the public methods, like this:
var Module = (function() {
var exports = {}, x = 1;
function private() {
return x;
}
exports.public = function() {
return x;
};
return exports;
}());
This seems like over the long term it may be a pain, so I was looking for an easier way. I've seen that I can call apply to the function, and pass in an object, then use "this" to reference it and return "this" for the same effect, like this:
var Module = (function() {
var x = 1;
function private() {
return x;
}
this.public = function() {
return x;
};
return this;
}).apply({});
Is there anything wrong with using apply and passing in an empty object? This seems like the best way as the "this" keyword goes nicely with JS Intellisense coloring, and seems to be easier to understand. Anybody see any problems with this or have a simpler way?