子对象访问父对象的更好方法?
我有一些 JS 你会在下面看到。我希望内部类对象能够访问它的父对象。它需要访问父方法和属性。我这样做的方式是有效的,但我想知道是否可以在内部类构造函数中做一些事情来获取父级,而不是父级必须显式告诉子级它的父级是谁。看起来很笨重。
<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>
运行的输出如下所示:
戴维森来自大卫
约翰逊来自约翰
我问是因为上述结构已经存在于我正在维护的项目中。我不能去重新设计整个事情。只是现在子对象必须访问它的父对象。以前不需要。如果根本不必更改父类并在子类中进行所有更改,那就太好了。但如果我所得到的基本上是“你需要做的”,那就这样吧。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
原则上,这已经是正确的方法了。您可以让它变得更奇特,在子级中提供一个 insertParent() 函数来检查父级引用是否已设置,但这不会改变问题的核心。
虽然数据模型中的子级只能属于一个父级,但 Javascript 并不知道这一点。例如,可能有多个父母提到同一个孩子(传闻自然界中有时会发生这种情况)。事实上,孩子不是父母的“内部阶级”。孩子和父母只是相关联的,即“他们以某种方式彼此认识”,这是一种未指定的关系,但两者都不是对方的一部分(或财产)。
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.
我的偏好是在创建子级时将父级传递给子级,而不是在创建子级后将父级设置为单独的步骤。因此,代码几乎与您的代码完全相同,除了 ChildClass 函数上有一个额外的参数:
当然,没有什么可以阻止您在创建子级后更改父级,或者通过不传递该参数来创建具有 null 或未定义父级的子级。
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:
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.
实现这一点的经典方式是通过事件。您可以实现一个从事件处理程序中获取信息的事件。
在一个好的设计中,如果您想要父级的“foo”值,那么任何实现“foo”的父级都有一个声明“foo”的共同祖先,并且祖先将是实现“foo”处理程序的唯一位置。
您现在所面临的引入了可怕的内聚力降低和耦合扩大。
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.