[Javascript] Create a Custom Iterator for Any Array
Posted Answer1215
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Javascript] Create a Custom Iterator for Any Array相关的知识,希望对你有一定的参考价值。
Using Symbol.iterator
, you can create custom iterators that can be used inside of for
loops and Array spreads. This lesson walks you through creating a function to create iterators from arrays that you pass into the function.
const abcs = ["A", "B", "C"] const numbers = [1, 2, 3] const createReverseIterator = array => ({ [Symbol.iterator]() { let i = array.length return { next: () => ({ value: array[--i], done: i < 0 }) } } }) for (let value of createReverseIterator(numbers)) { console.log(value) }
以上是关于[Javascript] Create a Custom Iterator for Any Array的主要内容,如果未能解决你的问题,请参考以下文章
[Javascript] Create a Custom Iterator for Any Array
How difficult is it to create a JavaScript framework?