I'm working with a pattern that looks like so (pseudo example):
var FOO = (function(foo) {
var foo = foo || {},
setThis = 'someValue';
//--------------------------------------------------------------------------
//
// Public methods:
//
//--------------------------------------------------------------------------
foo.init = function(bar) {
this.blah = [];
// Constructor stuff here...
};
foo.doSomething = function(bar) {
if (bar) {
this.doSomethingElse();
// Stuff here...
}
};
foo.doSomethingElse = function() {
// Stuff here...
};
//--------------------------------------------------------------------------
//
// Private methods:
//
//--------------------------------------------------------------------------
foo._imPrivate = function() {
// ... stuff here ...
this.blah = xyz; // References this.
};
foo._morePrivate = function(el) {
// No reference to this.
};
foo._otherPrivate = function(el) {
// No reference to this.
};
return foo; // Expose the methods.
}(FOO || {}));
Instanciated like so:
window.onload = function() { FOO.init(stuff); }
Three questions:
- If my "private" methods don't reference
this
, should I just make them "standard" functions (i.e.function _imPrivate() { ... }
, for example)? Reason why I ask: I have a few methods that referencethis
, but I don't want to give them public access; I also have a few "utility" methods that don't referencethis
... Can the methods that referencethis
be standard functions (in context of the module pattern)? - Could someone provide an example of how I would implement a setter for the
setThis
variable? - Do you see any room for improvements in the above code?