Iterator.from()

Experimental: This is an experimental technology
Check the Browser compatibility table carefully before using this in production.

Iterator.from() 静态方法从迭代器或可迭代对象创建新的 Iterator 对象。

¥The Iterator.from() static method creates a new Iterator object from an iterator or iterable object.

语法

¥Syntax

js
from(object)

参数

¥Parameters

object

实现 iterable 协议或 iterator 协议的对象。

返回值

¥Return value

如果 object 是一个可迭代对象,则调用其 @@iterator 方法来获取迭代器。否则,object 被假定为迭代器。如果迭代器已经是 instanceof Iterator(这意味着它的原型链中有 Iterator.prototype),则直接返回。否则,将创建一个新的 Iterator 对象来封装原始迭代器。

¥If object is an iterable, its @@iterator method is called to obtain the iterator. Otherwise, object is assumed to be an iterator. If the iterator is already instanceof Iterator (which means it has Iterator.prototype in its prototype chain), it is returned directly. Otherwise, a new Iterator object is created that wraps the original iterator.

描述

¥Description

此方法用于将可能由库导出的自定义迭代器转换为 适当的迭代器Iterator.from() 返回的所有迭代器对象都继承自一个公共原型对象,该对象具有以下方法:

¥This method exists to convert custom iterators, probably exported by libraries, to proper iterators. All iterator objects returned by Iterator.from() inherit from a common prototype object, which has the following methods:

next()

调用底层迭代器的 next() 方法并返回结果。

return()

调用底层迭代器的 return() 方法并返回结果,如果底层迭代器没有 return() 方法,则返回 { value: undefined, done: true }

示例

¥Examples

将可迭代对象转换为适当的迭代器

¥Converting an iterable to a proper iterator

由于 obj 已经是一个可迭代对象,当调用 @@iterator 方法时,它会返回正确的迭代器,因此 Iterator.from(obj) 返回相同的迭代器。

¥Because obj is already an iterable that returns a proper iterator when its @@iterator method is called, Iterator.from(obj) returns the same iterator.

js
const iterator = (function* () {
  yield 1;
  yield 2;
  yield 3;
})();

const obj = {
  [Symbol.iterator]() {
    return iterator;
  },
};

const iterator2 = Iterator.from(obj);
console.log(iterator2 === iterator); // true

因为 obj2 是一个迭代器,当调用 @@iterator 方法时,它会返回一个非正确的迭代器,所以 Iterator.from(obj2) 返回一个封装原始迭代器的新迭代器。

¥Because obj2 is an iterable that returns a non-proper iterator when its @@iterator method is called, Iterator.from(obj2) returns a new iterator that wraps the original iterator.

js
const iterator = {
  current: 0,
  next() {
    return { value: this.current++, done: false };
  },
};

const obj2 = {
  [Symbol.iterator]() {
    return iterator;
  },
};

const iterator2 = Iterator.from(obj2);
console.log(iterator2 === iterator); // false
console.log(iterator2.next()); // { value: 0, done: false }
console.log(iterator.next()); // { value: 1, done: false }

将迭代器转换为适当的迭代器

¥Converting an iterator to a proper iterator

因为 obj 已经是一个正确的迭代器,所以 Iterator.from(obj) 返回其自身。

¥Because obj is already a proper iterator, Iterator.from(obj) returns itself.

js
const obj = (function* () {
  yield 1;
  yield 2;
  yield 3;
})();

const iterator = Iterator.from(obj);
console.log(iterator === obj); // true

因为 obj2 是一个非正确迭代器,所以 Iterator.from(obj2) 返回一个封装原始迭代器的新迭代器。

¥Because obj2 is a non-proper iterator, Iterator.from(obj2) returns a new iterator that wraps the original iterator.

js
const obj2 = {
  current: 0,
  next() {
    return { value: this.current++, done: false };
  },
};

const iterator = Iterator.from(obj2);
console.log(iterator === obj2); // false
console.log(iterator.next()); // { value: 0, done: false }
console.log(obj2.next()); // { value: 1, done: false }

规范

Specification
Iterator Helpers
# sec-iterator.from

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看