bodokaiser
bodokaiser

Reputation: 15752

How to scope child to parent object?

I got following code:

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();
});

As you see I can't access the other namespace with this approach. Is there another way to access this.module somehow?

Regards

Upvotes: 1

Views: 168

Answers (3)

Zlatko
Zlatko

Reputation: 19569

As I've said in a comment, maybe you can add a that variable to the core object, so that the other functions have something common to get a hold on?

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);
      }
    };

});

Upvotes: 0

David G
David G

Reputation: 96810

How about setting a parameter for the object?:

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

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

core.sandbox.register( a );

Upvotes: 1

xdazz
xdazz

Reputation: 160843

Use core.module._modules

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

Upvotes: 0

Related Questions