Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Better way of child object accessing parent object?

I have some JS you'll see below. I want the inner class object to be able to access it's parent. It needs to access parent methods and properties. The way I've done it is working, but I'd like to know if there's something I can do in the inner class constructor to get the parent, rather than the parent having to explicitly tell the child who it's parent is. It seems clunky.

<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 output from running that looks like this:

Davidson came from David

Johnson came from John

I ask because the above structure already exists in the project I'm maintaining. I can't go redesigning the whole thing. It's only now that the child object has to access it's parent. Previously it hasn't needed to. It would be nice to not have to change the parent class at all and do all my changes within the child. But if what I've got is basically "what you need to do" then so be it.

like image 276
Dee2000 Avatar asked Jun 08 '11 21:06

Dee2000


2 Answers

This is already the correct way to do this, in principle. You could make it somewhat fancier, providing an introduceParent() function in the child which checks, if the parent reference is already set, but that doesn't change the core of the matter.

While a child in your data model can belong to only one parent, Javascript doesn't know that. There could be several parents referencing the same child, for example (hearsay is that sometimes happens in nature). Indeed, the child is not an "inner class" of the parent. The child and parent are merely associated, i.e. "they somehow know each other", which is an unspecified relation, but neither is part (or property) of the other.

like image 70
T-Bull Avatar answered Oct 27 '22 08:10

T-Bull


My preference would be to pass the parent to the child when the child is created, rather than setting the parent as a separate step after creating the child. So code almost exactly like yours except with an extra parameter on 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 there's nothing stopping you changing the parent after the child is created, or creating children with null or undefined parents by just not passing that parameter.

like image 39
nnnnnn Avatar answered Oct 27 '22 09:10

nnnnnn