I have seen the following style for returning a public interface from a constructor:
function AnObjectConstructor() {
function localFunc() {};
function publicFunc() {};
// public interface
var _this = {
publicFunc: publicFunc
};
return _this;
};
I like this style because the public interface is clearly in one place and also localFunc can access publicFunc without using _this
This code can also be written like this:
function AnObjectConstructor() {
function localFunc() {};
function publicFunc() {};
// public interface
var _this = this;
_this.publicFunc = publicFunc;
return _this;
};
Both these forms are intended to be used with new:
var obj = new AnObjectConstructor();
obj.publicFunc();
I am not clear on whether these two forms are the same or not, and was hoping someone can help me with that ?
Also any general comments on this style will be appreciated..