JavaScript对象来自另一个对象(JavaScript objects dependency from another objects)

编程入门 行业动态 更新时间:2024-10-11 05:30:57
JavaScript对象来自另一个对象(JavaScript objects dependency from another objects)

有人可以解释或告诉我如何合并两个JavaScript对象,也许我可以称之为小部件,然后一起工作。

假设我有一个大对象( 窗口 )代表一个资源管理器窗口和一个较小的对象( 工具栏 ),它代表带有各种按钮的工具栏。 单独操作它们(添加,删除,更改属性等)我没有问题。 但是我如何让它们一起工作呢? 例如,如果我想以这种方式从窗口中删除工具栏 window.Toolbar.Remove()

Remove()函数是toolbar对象的一部分,它将从DOM中删除它自己的工具栏 ,但是如何从window对象中删除它的引用并将其设置为null 。 我应该破坏这个toolbar吗?

如果我破坏主要父对象window ,我应该如何销毁所有较小的对象,如toolbar ?

任何指针高度赞赏。

Could someone explain or show me how to incorporate two JavaScript objects, maybe I can call them widgets, to work then together.

Lets say I have a big object (window) which represents a explorer window and a smaller object (toolbar) which represent toolbar with various buttons. Manipulating them alone (add, delete, change properties and etc.) I have no problems. But how do I make them work together? For example, if I want to remove toolbar from window in this way window.Toolbar.Remove()

Remove() function is part of toolbar object and would remove toolbar it self from DOM, but how do I remove it's reference from window object and set it to null. Should I destroy this toolbar some how?

And if I destroy main parent object window, how should I destroy all smaller objects like toolbar?

Any pointers highly appreciated.

最满意答案

我在这里看到两种可能的选择:

1)使子窗口小部件知道父窗口。

你可以有这样的东西(伪代码):

window.addChild(name, WigdetClass) { this[name] = new WigdetClass(this); } // add toolbar window.addChild('toolbar', Toolbar); // Toolbar constructor accepts `parent` as parameter Toolbar(parent) { this.parent = parent; } Toolbar.prototype.Remove() { // remove self from the page ... // set parent reference to null parent.toolbar = null; }

2)或者您可以将“删除”移动到父对象:

Window.prototype.RemoveChild(child) { // remove child from the document ... var propertyName = this.findChildName(child); // 'toolbar' this[propertyName] = null; }

I see two possible options here:

1) Make the child widget aware of parent.

You can have something like this (pseudo-code):

window.addChild(name, WigdetClass) { this[name] = new WigdetClass(this); } // add toolbar window.addChild('toolbar', Toolbar); // Toolbar constructor accepts `parent` as parameter Toolbar(parent) { this.parent = parent; } Toolbar.prototype.Remove() { // remove self from the page ... // set parent reference to null parent.toolbar = null; }

2) Or you can move "Remove" to the parent object:

Window.prototype.RemoveChild(child) { // remove child from the document ... var propertyName = this.findChildName(child); // 'toolbar' this[propertyName] = null; }

更多推荐

本文发布于:2023-07-18 07:42:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1157124.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对象   JavaScript   dependency   objects

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!