How do you have multiple child objects of a parent object? Consider the following object declaration:
var parent={
childCount: 0,
child: {
id:false,
init: function() {
alert(this.id);
parent.childCount++;
this.id = parent.childCount;
alert("Child " + this.id + " Created");
}
}
}
Calling 'parent.child.init();', the first alert produces 'undefined' and the second 'Child 1 Created'. A second call to 'parent.child.init();' produces the '1' from above instead of the 'undefined' I'm expecting - I realise this is because I'm working with the same object.
However, calling 'var firstChild = parent.child.init(); var secondChild = parent.child.init()' produces the same result (as does 'var firstChild = parent.child; firstChild.init().
I've also tried the 'new' operator. But the code 'var firstChild = new parent.child;' produces the error 'parent.child is not a constructor'.
How is this supposed to work?
Thanks,
Iain
var parent={
childCount: 0,
child: {
id:false,
init: function() {
alert(this.id);
parent.childCount++;
this.id = parent.childCount;
alert("Child " + this.id + " Created");
}
}
}
Calling 'parent.child.init();', the first alert produces 'undefined' and the second 'Child 1 Created'. A second call to 'parent.child.init();' produces the '1' from above instead of the 'undefined' I'm expecting - I realise this is because I'm working with the same object.
However, calling 'var firstChild = parent.child.init(); var secondChild = parent.child.init()' produces the same result (as does 'var firstChild = parent.child; firstChild.init().
I've also tried the 'new' operator. But the code 'var firstChild = new parent.child;' produces the error 'parent.child is not a constructor'.
How is this supposed to work?
Thanks,
Iain