Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[zh-cn]: Update translation of String.@@iterator() #14045

Merged
merged 2 commits into from
Jul 6, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,45 @@ slug: Web/JavaScript/Reference/Global_Objects/String/@@iterator

{{JSRef}}

**`[@@iterator]()`** 方法返回一个新的 Iterator 对象,它遍历字符串的代码点,返回每一个代码点的字符串值
{{jsxref("String")}} 类型的 **`[@@iterator]()`** 方法实现了[可迭代协议](/zh-CN/docs/Web/JavaScript/Reference/Iteration_protocols),并允许字符串与大多数期望传入可迭代对象的语法一起使用,例如[展开语法](/zh-CN/docs/Web/JavaScript/Reference/Operators/Spread_syntax)和 {{jsxref("Statements/for...of", "for...of")}} 循环。它返回一个[字符串迭代器对象](/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Iterator),它按 Unicode 码位迭代字符串值并以字符串的形式返回

{{EmbedInteractiveExample("pages/js/string-iterator.html")}}

The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone <https://github.com/mdn/interactive-examples> and send us a pull request.

## 语法

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

### 返回值

一个新的 Iterator 对象
一个新的[可迭代迭代器对象](/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Iterator),它以字符串值中的 Unicode 码位生成单独的字符串

## 示例
## 描述

### 使用 `[@@iterator]()`
按 Unicode 码位迭代字符串。这意味着会将字素簇拆分,但代理对将被保留。

```js
var string = 'A\uD835\uDC68';
// "Backhand Index Pointing Right: Dark Skin Tone"
[..."👉🏿"]; // ['👉', '🏿']
// splits into the basic "Backhand Index Pointing Right" emoji and
// the "Dark skin tone" emoji

// "Family: Man, Boy"
[..."👨‍👦"]; // [ '👨', '‍', '👦' ]
// splits into the "Man" and "Boy" emoji, joined by a ZWJ
```

var strIter = string[Symbol.iterator]();
## 示例

console.log(strIter.next().value); // "A"
console.log(strIter.next().value); // "\uD835\uDC68"
```
### 使用 for...of 循环进行迭代

### 通过 `for..of` 使用 `[@@iterator]()`
请注意,你很少需要直接调用该方法。`@@iterator` 方法的存在使得字符串[可迭代](/zh-CN/docs/Web/JavaScript/Reference/Iteration_protocols#可迭代协议),而像 `for...of` 循环这样的迭代语法会自动调用该方法以获取迭代器进行循环。

```js
var string = 'A\uD835\uDC68B\uD835\uDC69C\uD835\uDC6A';
const str = "A\uD835\uDC68B\uD835\uDC69C\uD835\uDC6A";

for (var v of string) {
for (const v of str) {
console.log(v);
}
// "A"
Expand All @@ -50,6 +54,19 @@ for (var v of string) {
// "\uD835\uDC6A"
```

### 手动迭代

你仍然可以手动调用返回的迭代器对象的 `next()` 方法,以实现对迭代过程最大程度的控制。

```js
const str = "A\uD835\uDC68";

const strIter = str[Symbol.iterator]();

console.log(strIter.next().value); // "A"
console.log(strIter.next().value); // "\uD835\uDC68"
```

## 规范

{{Specifications}}
Expand All @@ -58,6 +75,7 @@ for (var v of string) {

{{Compat}}

## 相关链接
## 参见

- [Iteration protocols](/zh-CN/docs/Web/JavaScript/Reference/Iteration_protocols)
- [`core-js` 中 `String.prototype[@@iterator]` 的 polyfill](https://github.com/zloirock/core-js#ecmascript-string-and-regexp)
- [迭代协议](/zh-CN/docs/Web/JavaScript/Reference/Iteration_protocols)