When I switched to the revealing module pattern in Javascript, it was a sigh of relief. No more this, that, exports.func, that.func.bind(that), function() { that.success(); }, etc. In fact, in my modules no more occurrences of 'this' even exist, hence less confusion and more clarity! If only I could also apply the same concept to a class. This would allow me to work within the private scope of the class, and reveal only public functions at the bottom. Here is my attempt to do this, is it even a class? Do you see any problems with this implementation?
var TestClass = function(_a) {
var a = _a;
function func() { console.log(a); }
return {
func: func
}
};
var tc1 = new TestClass(1);
var tc2 = new TestClass(2);
tc1.func(); //1
tc2.func(); //2
EDIT: I call it the 'Revealing Class Pattern' (w3core below gave me this idea)
var TestClass = function(_a) {
var a = _a;
function func() {
console.log(a);
}
this.func = func;
};
TestClass.prototype.shared = function() {
console.log(5);
};
var tc1 = new TestClass(1);
var tc2 = new TestClass(2);
tc1.func(); //1
tc2.func(); //2
tc1.shared(); //5
tc2.shared(); //5
tc1.constructor; //function TestClass()
tc1 instanceof TestClass; //true
This method seems to fix the 'instanceof' problem as mentioned in the comments. Also, the constructor is now set to the function as well.