在javascript中使用树遍历方法扩展对象
我有一个自定义对象,其中包含一个数组(称为“子项”),其中将存储相同类型的对象,从而创建一棵树。
假设它看起来像这样:
function CustomObject(){
if (this instanceof Topic) {
this.text = "Test";
this.children = [];
} else
return new CustomObject(); }
现在,我想向该对象添加一个“forAll”方法,该方法将以深度优先的方式对该树的所有元素执行作为参数提供的另一个函数。最好的方法是什么?
I have a custom object that contains an array (called "children") where objects of the same type will be stored, in result creating a tree.
Let's say it looks like that:
function CustomObject(){
if (this instanceof Topic) {
this.text = "Test";
this.children = [];
} else
return new CustomObject(); }
Now, I would like to add a "forAll" method to this object that would execute another function provided as an argument, on all elements of that tree in a depth-first fashion. What's the best way to do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
像这样的东西吗?
Something like this?