Using the construction below, you can have private variables, public and private functions. So why do you have all sorts of ways to create a namespace?
Is NameSpace radically different from a function with corresponding behavior and scope?
I see that do not pollute the global namespace, for example. window in browsers with many features that could be created, but this can be achieved even lower.
It seems that I am missing a fundamental point.
// Constructor for customObject function customObject(aArg, bArg, cArg) { // Instance variables are defined by this this.a = aArg; this.b = bArg; this.c = cArg; } // private instance function customObject.prototype.instanceFunctionAddAll = function() { return (this.a + this.b + this.c); } /* Create a "static" function for customObject. This can be called like so : customObject.staticFunction */ customObject.staticFunction = function() { console.log("Called a static function"); } // Test customObject var test = new customObject(10, 20, 30); var retVal = test.instanceFunctionAddAll(); customObject.staticFunction();
source share