javascript 具有iterables的反向循环数组。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript 具有iterables的反向循环数组。相关的知识,希望对你有一定的参考价值。

class IterableBackwards {
  constructor(xs) {
    this._xs = xs;
    this._i = xs.length;
  }

  [Symbol.iterator]() { return this; }

  next() {
    return (this._i <= 0)
      ? { done: true, value: undefined }
      : { done: false, value: this._xs[--this._i] };
  }
}

const xs = ['A1', 'A2', 'A3'];

for (const x of new IterableBackwards(xs)) {
  console.info(x);
}

以上是关于javascript 具有iterables的反向循环数组。的主要内容,如果未能解决你的问题,请参考以下文章