I've seen the revealing module pattern, as well as the prototype class pattern. I kind of adapted the two together into a revealing class pattern. I'm trying to figure out if there are any gotchas with this pattern, or things that wouldn't work. As long as the constructor for the object is typeof "function" I would think there wouldn't be any problems.
The only caveat I can come up with, is a high volume instantiated class, where the private functions are getting create every time a new class is created. This may cause memory problems for thousands of instantiations. For larger classes that don't build up in memory, though, my thinking is the easier code readability and security may be of benefit. By keeping functions private and revealing them, the obfuscator minimizes all of the internal functions, making it difficult for prying eyes. Granted, it is not foolproof, but just an extra layer of protection. Additionally, being able to work inside of the function privately, removes hundreds of occurrences of "this" within the class. Maybe it's a small advantage, but for complex code it kind of improves the readability. Anybody see any major problems with the pattern?
//standard pattern
var MyClass = function() {
this.func1 = function() {
//dostuff
};
};
MyClass.prototype.func2 = function() {
this.func1();
//dostuff
};
-
//revealing class pattern
var MyClass = function() {
function privateFunc() {
//dostuff
}
function publicFunc() {
privateFunc();
//dostuff
}
this.publicFunc = publicFunc;
};