salmonelopan
3/21/2019 - 3:58 PM

generator and recursion with a tree

generator and recursion with a tree

class Comment {
  constructor(content, children){
    this.children = children;
    this.content = content;
  }

  *[Symbol.iterator](){
    yield this.content;
    for (let child of this.children){
      yield* child;
    }
  }
}

const children = [
  new Comment('good content', []),
  new Comment('bad comment', []),
  new Comment('hey this has children', [ new Comment('child', [])])
];

const tree = new Comment('Great Post', children);

const values = [];

for (let value of tree){
  values.push(value);
}

console.log(values);