Skip to content

Commit

Permalink
[zh-cn]: add the translation of iterator.@@iterator() (#18008)
Browse files Browse the repository at this point in the history
Co-authored-by: A1lo <yin199909@aliyun.com>
  • Loading branch information
JasonLamv-t and yin1999 authored Jan 24, 2024
1 parent 39c763f commit 0dc27d3
Showing 1 changed file with 52 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
---
title: Iterator.prototype[@@iterator]()
slug: Web/JavaScript/Reference/Global_Objects/Iterator/@@iterator
l10n:
sourceCommit: 27180875516cc311342e74b596bfb589b7211e0c
---

{{JSRef}}

{{jsxref("Iterator")}} 实例的 **`[@@iterator]()`** 方法实现了[可迭代协议](/zh-CN/docs/Web/JavaScript/Reference/Iteration_protocols),并允许内置迭代器被大多数期望可迭代对象的语法所消费,如[展开语法](/zh-CN/docs/Web/JavaScript/Reference/Operators/Spread_syntax)和 {{jsxref("Statements/for...of", "for...of")}} 循环。它返回 [`this`](/zh-CN/docs/Web/JavaScript/Reference/Operators/this) 的值,即迭代器对象本身。

## 语法

```js-nolint
iterator[Symbol.iterator]()
```

### 参数

无。

### 返回值

[`this`](/zh-CN/docs/Web/JavaScript/Reference/Operators/this) 的值,即迭代器对象本身。

## 示例

### 使用 for...of 循环进行迭代

请注意,你很少需要直接调用这个方法。`@@iterator` 方法的存在使内置迭代器成为[可迭代](/zh-CN/docs/Web/JavaScript/Reference/Iteration_protocols#可迭代协议)对象,而像 `for...of` 循环这样的迭代语法会自动调用这个方法来获取用于进行循环的迭代器。

```js
const arrIterator = [1, 2, 3].values();
for (const value of arrIterator) {
console.log(value);
}
// 输出:1、2、3
```

## 规范

{{Specifications}}

## 浏览器兼容性

{{Compat}}

## 参见

- {{jsxref("Iterator")}}
- {{jsxref("Symbol.iterator")}}
- [迭代协议](/zh-CN/docs/Web/JavaScript/Reference/Iteration_protocols)

0 comments on commit 0dc27d3

Please sign in to comment.