使用以下构造,您可以拥有私有变量、公共和私有函数。那么为什么有各种不同的方法来创建命名空间呢?
NameSpace 是否与具有相关行为和范围的函数完全不同?
我看到了不污染全局命名空间的意义,例如浏览器中的窗口对象,它会创建过多的功能,但这也可以通过以下方式实现..
似乎我错过了一个基本点..
// 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();