アプリの名前空間が次のようになっている場合:
var myApp = {};
(function() {
var id = 0;
this.next = function() {
return id++;
};
}).apply(myApp);
次に、次の結果をログに記録すると:
console.log(myApp.next()); //1
たとえば、次のような名前空間関数内に変数を格納するにはどうすればよいですか。
var myApp = {};
(function() {
var id = 0;
this.next = function() {
return id++;
};
// Store variables here ...
this.variableStore = function() {
var var1 = "One";
};
}).apply(myApp);
このようにアクセスしようとしています:
console.log(myApp.variableStore().var1); // Gives me an error
これは可能ですか、それとも良い考えですか?それとも、本質的にグローバル変数である新しい名前空間を宣言する必要がありますか?