Is it possible to define an object within another object? I'm thinking something like this:
function MyObj(name) {
this.name = name;
function EmbeddedObj(id) {
this.id = id;
}
}
And I could then create an EmbeddedObj like this:
var myEmbeddedObj = new MyObj.EmbeddedObj();
Meme for bonus points: Objectception! :o
Does that look like what you're after?
Here is example of nested constructor.
I guess this is what you mean by nested object constructor.
Yes, and no.
Would run, but it might not yield the expected results for "embedded object" (see comment).
Note that in the case of
new expr
the expression is evaluated first so, in this case it creates a new object using the function-object evaluated fromMyObject.EmbeddedObj
as a constructor. (There is a silly rule with parenthesis in the expression, but that's another story.)Now, if a "parent" and "child" relationship was desired, that could be done, using a more round-about method: