Extending an object with a tree-traversing method in javascript -
i have custom object contains array (called "children") objects of same type stored, in result creating tree.
let's looks that:
function customobject(){ if (this instanceof topic) { this.text = "test"; this.children = []; } else return new customobject(); }
now, add "forall" method object execute function provided argument, on elements of tree in depth-first fashion. what's best way it?
something this?
customobject.prototype.forall = function(func) { // process object first func(this); // process children (var = 0; < this.children.length; i++) this.children[i].forall(func); }
Comments
Post a Comment