开发者

Better way of child object accessing parent object?

开发者 https://www.devze.com 2023-03-11 19:21 出处:网络
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

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 ChildCla开发者_如何学编程ss(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.


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.


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.


The classical way for this to take place is with events. You can implement an event that elicits info from the event handler.

In a good design, if you want the parent's "foo" value, then any parents that implement "foo" have a common ancestor that declares "foo", and the ancestor would be the single place to implement your "foo"-handler.

What you have now introduces the dreaded cohesion reduction and coupling enlargement.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号