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.
source share