Best way to access child object to parent object?

I have JS, which you will see below. I want the internal class object to be able to access the parent. It must access the parent methods and properties. The way I did this works, but I would like to know if there is something that I can do in the internal constructor of the class to get the parent, instead of the parent having to explicitly tell the child who the parent is. It seems awkward.

<html> <body> <script> function ChildClass(name){ //this.myParent= no way of knowing ..... this.myName=name; this.whereDidIComeFrom=function(){ document.write(this.myName+ " came from " +this.myParent.dad+"<br>"); } } function ParentClass(name){ this.dad=name; this.myChild=new ChildClass(name+"son"); this.myChild.myParent=this; //only way I know for myChild to access parent this.myChild.whereDidIComeFrom(); } var parentDavid = new ParentClass("David"); var parentJohn = new ParentClass("John"); </script> </body> </html> 

The result of the work looks like this:

Davidson came from david

Johnson came from John

I ask because the above structure already exists in the project that I support. I can’t redo it all. This is only now when the child must get a parent to it. This was not required before. It would be nice not to change the parent class at all and make all my changes inside the child. But if what I have is basically “what you need to do,” it should be.

+6
source share
3 answers

This is the right way to do this, in principle. You can make it a little more attractive by providing a childParent () function in the child element, which checks if the parent link is already set, but that does not change the core of the question.

As long as the child in your data model can only belong to one parent, Javascript does not know this. Perhaps several parents refer to the same child, for example (rumors that sometimes occur in nature). Indeed, the child is not the “inner class” of the parent. The child and the parent are simply connected to each other, that is, "they somehow know each other," which is an indefinite relationship, but is not part (or property) of the other.

+5
source

My preference was to pass the parent to the child when it was created, instead of setting the parent as a separate step after creating the child. So the code is almost the same as yours, except for an additional parameter in the ChildClass function:

 <script> function ChildClass(name,myParent){ this.myParent=myParent; this.myName=name; this.whereDidIComeFrom=function(){ if (this.myParent != undefined && this.myParent.dad != undefined) document.write(this.myName+ " came from " +this.myParent.dad+"<br>"); else document.write(this.myName+ " has no father<br>"); } } function ParentClass(name){ this.dad=name; this.myChild=new ChildClass(name+"son",this); this.myChild.whereDidIComeFrom(); } var parentDavid = new ParentClass("David"); var parentJohn = new ParentClass("John"); 

Of course, nothing prevents you from changing the parent after creating the child, or creating children with parents with zero or undefined, simply without passing this parameter.

+1
source

The classic way to do this is with events. You can implement an event that outputs information from an event handler.

In a good design, if you want the parent value "foo", then all parents who implement "foo" have a common ancestor that declares "foo", and the ancestor will be the only place to implement your "foo", -handler.

Now you have a terrible reduction in grip and increased communication.

0
source

Source: https://habr.com/ru/post/890096/


All Articles