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?
Something like this?