Could you explain me how to do in JavaScript such thing :
- Create one object.
- Create second object.
- Set first object as a parent to second object.
- Add second object as child to first object.
It sounded simple to me as Java developer but I am confused right now.
Demo:
var childFunction = function(parent){
this.parent = parent;
this.value = 14;
this.fun = function(){
document.getElementById("test").innerHTML=parent.value;
};
this.setParent = function(parent){
this.parent = parent;
}
}
var parentFunction = function(){
this.value=20;
this.child='';
this.addChild = function(child){
child.setParent(this);
this.child=child;
}
this.setchild = function(child){
this.child = child;
}
this.createChild= function(){
this.child = new childFunction(this);
}
}
var par = new parentFunction();
var ch = new childFunction('');
//par.setchild(new childFunction(par));
//par.createChild();
par.addChild(ch);
par.child.fun();
<div id="test"></div>
thisreferences in each case, you might be surprised.