0

I am having a little trouble structuring some code in JavaScript, below is an example of how I would like to use the code:

var instance = new myObject("foo", "bar");
var sub = new myObject.subObject("x", "y", "z");
instance.doSomething();

The subObject would not have access to properties of myObject (such as "foo" and "bar", in this case), rather myObject sort of simply encapsulates subObject, if you see what I mean. In this example "x", "y" and "z" are local to the instance of myObject.subObject.

Functions within myObject, such as doSomething() may themselves create some instances of myObject.subObject, and would have access to properties of myObject, such as "foo" and "bar".

So what structural form does myObject take?

Edit:

I was thinking something along these lines:

function myObject(foo, bar) {
    this.foo = foo;
    this.bar = bar;

    this.doSomething = function() {
        var a = new this.subObject("x", "y", "z");
        var b = new this.subObject("1", "2", "3");
        console.log(a.x, a.y, a.z, b.x, b.y, b.z);
        };

    this.subObject = function(x, y, z) {
        this.x = x;
        this.y = y;
        this.z = z;
        };
}
Testic
  • 157
  • 1
  • 1
  • 9
  • I'm not sure I understand completely, and I'm also not sure you understand completely. In the second line, you're creating a whole new instance of `myObject`, so that instance of `myObject` won't have access to anything in the first instance created anyways. What I'm getting from the above is: you want a `subObject` method that can't access it's parent instance, but also other instances part of `myObject` that can. – jeremy Jul 02 '14 at 14:58
  • how is myObject defined? – Kevin B Jul 02 '14 at 14:58
  • @KevinB he wants us to do that. See the last line – Amit Joki Jul 02 '14 at 14:59
  • I would expect it to be a function that has a prototype and a subObject property that also has a prototype. but.... without knowing what this is for or how it is goign to be used, it seems kinda pointless to write any code for it. It could be done in so many different ways. – Kevin B Jul 02 '14 at 15:00
  • 2
    `myObject.subObject` would just be a normal constructor function. There is nothing special you have to do to "islolate" properties. `instance` will never know about `sub` and vice versa. – Felix Kling Jul 02 '14 at 15:01
  • 1
    Your solution doesn't match with the example you provided. In your first example, `subObject` is a property of the **constructor**, not a property of an **instance**. – Felix Kling Jul 02 '14 at 15:08
  • @Bergi: The other question seems to be a little different, since the "inner constructor" is a property of the *instance* of the outer constructor, which is not the case here. – Felix Kling Jul 02 '14 at 15:10
  • @FelixKling: Oh, right. Didn't notice that. Though it might have been a mistake and is what the OP actually wanted. Reopening nonetheless. – Bergi Jul 02 '14 at 15:20
  • I think at least part of the problem is my own confusion. I have accepted @FelixKling's answer as it does what I want, even my question was unclear. – Testic Jul 02 '14 at 15:25

2 Answers2

1

myObject.subObject would just be a normal constructor function. There is nothing special you have to do to "islolate" properties. instance will never know about sub and vice versa. I'm capitalizing constructor names so that it's clearer what is an instance and what is a cosntructor:

function MyObject(a, b) {
    // do stuff with a and b
}
MyObject.prototype.doSomething = function() {
    var innerSub = new MyObject.SubObject();
    // or 
    // var innerSub = new this.constructor.SubObject();
};

MyObject.SubObject = function(x, y, z) 
    // do stuff with x,y and z
};


var instance = new MyObject("foo", "bar");
var sub = new MyObject.SubObject("x", "y", "z");
instance.doSomething();

There really isn't anything special about MyObject.SubObject`. Since functions are objects themselves, you can just add properties to them like you want to.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • This looks like what I was trying to do. In your code could I have something like `var a = new this.SubObject(x, y, z)` within doSomething()? – Testic Jul 02 '14 at 15:10
  • 1
    You would do it as I showed: `new MyObject.SubObject()`. Or you could do `new this.constructor.SubObject()`. All you need is a reference to `MyObject` somehow. `this` refers to the **instance**, not the **constructor**. – Felix Kling Jul 02 '14 at 15:12
  • Ah OK, I think I have grasped it now. – Testic Jul 02 '14 at 15:16
0

I believe you're looking for composition.

If you've an object A which has a B inside, because we said has a, we're talking about an association of objects.

You might create A and B constructors:

var A = function() { };

var B = function(associatedA) {
   this.parent = associatedA;
};

var instanceOfB = new B(new A());

// This provides access to "A" instance
var parent = instanceOfB.parent;

If you want to create instances of B within A, you should find no issue in there, because it's about creating an instance of A using regular new operator.

Matías Fidemraizer
  • 63,804
  • 18
  • 124
  • 206