I think you should omit new in nested object. It should be simple function:
function MyObject(){
this.myProp;
this.myMethod = function(){};
this.MyNestedObject = function(){ //no need of `new`
this.myNestedProp = 5;
this.myNestedMethod = function(){};
}
}
obj = new MyObject();
myNestedObj1 = new obj.MyNestedObject(); //better user brackets here too
myNestedObj2 = new obj.MyNestedObject();
myNestedObj3 = new obj.MyNestedObject();
You will be declaring nested object (or if you prefer OOP terminology then nested class) as a simple function, which when you call with new acts as a constructor resolving any this references inside the function pointing to newly constructed object. Thus essentially when you call **new** obj.MyNestedObject();
this.myNestedProp = 5;
adds myNestedProp property on newly created object (or this object). Similarly,
this.myNestedMethod = function(){};
adds myNestedMethod function on newly created object (or this object).
If you omit new in
myNestedObj1 = new obj.MyNestedObject();
Then MyNestedObject() will be invoked in the context of MyObject(). Thus this inside obj.MyNestedObject() will point to MyObject(). Thus before the execution of this above line, both MyObject() and obj will have three members: myProp, myMethod and MyNestedObject, but after execution of this call, it will add myNestedProp and myNestedMethod to the definition of MyObject() which in turn also appear inside obj.
