我得到以下代码:
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 吗?
问候
最佳答案
为对象设置参数怎么样?:
core.sandbox = {
register: function(obj, name) {
console.log( obj.module );
}
};
var a = new core.module.load(/* ..., .... */);
core.sandbox.register( a );
关于javascript - 如何将子对象的范围限制到父对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12543407/