生成器和树的递归

javascript
阅读 40 收藏 0 点赞 0 评论 0

generatorAndRecursion.js
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);
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号