以下の構成を使用すると、プライベート変数、パブリックおよびプライベート関数を持つことができます。では、なぜ名前空間を作成するためのさまざまな方法がすべてあるのでしょうか。
名前空間は、関連する動作とスコープを持つ関数とは根本的に異なりますか?
グローバル名前空間、たとえばブラウザのウィンドウオブジェクトを、作成する関数が多すぎることで汚染しないという点がわかりますが、それは以下でも実現できます。
基本的なポイントが欠けているようです。
// 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();