javascript - 如何将子对象的范围限制到父对象?

标签 javascript object scope prototype

我得到以下代码:

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/

相关文章:

json - 如何在 Angular 中连接两个对象?

c++ - 没有对象不能调用成员函数

自动和作用域的 C++ 初始化

python - 从 C++ 中在本地范围内创建 Python 对象

javascript - HOC import TypeError : Object(. ..) 不是函数

javascript - 使用矩阵围绕中心旋转 Svg 路径

javascript - Angular 4 验证码不工作

javascript - 如何使用变量内容读取对象数组的内容

javascript - Javascript 中的变量范围(提升)

javascript - Angular 表达的动态变化不起作用