0

Got a class like this:

class Item {
  constructor (next = null) {
    this.next = next
  }

  get self () { return this; }

  *[Symbol.iterator] () {
    yield this;
    if (this.next) yield *this.next;
  }
}

What is basically a linked list. Lets assume the list has at least three items and that root holds a reference to the head.

Right now, I can iterate like that and have a reference to both, the current item itself and its successor.

for (let { self, next } of root) {

}

Can the above be done without having the »self getter« withing the class, with destructuring only? So I have tried for (let {this, next} of root) but that does not work.

So can I have a reference to the »source object« with destructuring ?

philipp
  • 15,947
  • 15
  • 61
  • 106

0 Answers0