/ /子を親オブジェクトにスコープする方法は? -javascript、オブジェクト、スコープ、プロトタイプ

子オブジェクトから親オブジェクトへのスコープを設定する方法 - javascript、オブジェクト、スコープ、プロトタイプ

私は次のコードを得ました:

define(function() {

var core = function() {};
var module;

core.prototype.module = {
setModule: function(name) {
this.module = name;
module = name;
}
};

core.prototype.sandbox = {
getModules: function() {
// will throw undefined
console.log(this._module);
// will maybe not throw an error?
console.log(modules);
}
};

});

define(["kernel"], function(Kernel) {
var sampleCore = new Core();
sampleCore.setModule("test");
sampleCore.getModules();
});

ご覧のとおり、このアプローチでは他のネームスペースにアクセスできません。this.moduleにアクセスする別の方法はありますか?

よろしく

回答:

回答№1は1

オブジェクトのパラメータを設定するのはどうですか?:

core.sandbox = {
register: function(obj, name) {
console.log( obj.module );
}
};

var a = new core.module.load(/* ..., .... */);

core.sandbox.register( a );

回答№2の場合は0

つかいます core.module._modules

core.sandbox = {
register: function(name) {
var modules = core.module._modules;
}
};

回答№3の場合は0

私がコメントで言ったように、多分あなたは追加することができます それ コアオブジェクトに変数を追加して、他の関数に共通の何かを持たせます。

define(function() {
var core = function() {
var that = this;
};
var module;
core.prototype.module = {
setModule: function(name) {
that.module = name;
module = name;
}
};

core.prototype.sandbox = {
getModules: function() {
console.log(that.module);
}
};

});