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-tw] sync translated content #22439

Merged
merged 2 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions files/zh-tw/_redirects.txt
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,7 @@
/zh-TW/docs/Web/JavaScript/Obsolete_Pages/Obsolete_Pages/Obsolete_Pages/預先定義的函數/isFinite_函數 /zh-TW/docs/Web/JavaScript/Guide/Functions
/zh-TW/docs/Web/JavaScript/Obsolete_Pages/Obsolete_Pages/Obsolete_Pages/預先定義的函數/isNaN_函數 /zh-TW/docs/Web/JavaScript/Guide/Functions
/zh-TW/docs/Web/JavaScript/Obsolete_Pages/Obsolete_Pages/Obsolete_Pages/預先定義的核心物件 /zh-TW/docs/Web/JavaScript/Guide
/zh-TW/docs/Web/JavaScript/Reference/Global_Objects/Array/@@iterator /zh-TW/docs/Web/JavaScript/Reference/Global_Objects/Array/Symbol.iterator
/zh-TW/docs/Web/JavaScript/Same_origin_policy_for_JavaScript /zh-TW/docs/Web/Security/Same-origin_policy
/zh-TW/docs/Web/JavaScript/Shells /zh-TW/docs/Web/JavaScript/JavaScript_technologies_overview
/zh-TW/docs/Web/JavaScript/Typed_arrays /zh-TW/docs/Web/JavaScript/Guide/Typed_arrays
Expand Down
2 changes: 1 addition & 1 deletion files/zh-tw/_wikihistory.json
Original file line number Diff line number Diff line change
Expand Up @@ -3440,7 +3440,7 @@
"lizlux"
]
},
"Web/JavaScript/Reference/Global_Objects/Array/@@iterator": {
"Web/JavaScript/Reference/Global_Objects/Array/Symbol.iterator": {
"modified": "2020-10-15T22:02:20.291Z",
"contributors": ["jackblackevo"]
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ var myArray = myRe.exec("cdbBdbsbz");

- Array.length
- : `Array` 建構子的長度為 1。
- {{jsxref("Array.@@species", "get Array[@@species]")}}
- [`Array[Symbol.species]`](/zh-TW/docs/Web/JavaScript/Reference/Global_Objects/Array/Symbol.species)
- : 用來建立衍生物件的建構函數。
- {{jsxref("Array.prototype")}}
- : 可加入屬性至所有陣列物件。
Expand All @@ -163,7 +163,7 @@ var myArray = myRe.exec("cdbBdbsbz");

- {{jsxref("Array.prototype.length")}}
- : Reflects the number of elements in an array.
- {{jsxref("Array/@@unscopables", "Array.prototype[@@unscopables]")}}
- [`Array.prototype[Symbol.unscopables]`](/zh-TW/docs/Web/JavaScript/Reference/Global_Objects/Array/Symbol.unscopables)
- : Contains property names that were not included in the ECMAScript standard prior to the ES2015 version and that are ignored for [`with`](/zh-TW/docs/Web/JavaScript/Reference/Statements/with) statement-binding purposes.

### 方法
Expand Down Expand Up @@ -240,7 +240,7 @@ var myArray = myRe.exec("cdbBdbsbz");
- : Adds one or more elements to the front of an array, and returns the new `length` of the array.
- {{jsxref("Array.prototype.values()")}}
- : Returns a new [_array iterator_](/zh-TW/docs/Web/JavaScript/Guide/Iterators_and_Generators) object that contains the values for each index in the array.
- [`Array.prototype[@@iterator]()`](/zh-TW/docs/Web/JavaScript/Reference/Global_Objects/Array/@@iterator)
- [`Array.prototype[Symbol.iterator]()`](/zh-TW/docs/Web/JavaScript/Reference/Global_Objects/Array/Symbol.iterator)
- : An alias for the [`values()`](/zh-TW/docs/Web/JavaScript/Reference/Global_Objects/Array/values) method by default.

## 範例
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
---
title: Array.prototype[@@iterator]()
slug: Web/JavaScript/Reference/Global_Objects/Array/@@iterator
title: Array.prototype[Symbol.iterator]()
slug: Web/JavaScript/Reference/Global_Objects/Array/Symbol.iterator
---

{{JSRef}}

**`@@iterator`** 屬性的初始值與 {{jsxref("Array.prototype.values()", "values()")}} 屬性的初始值為相同的的函式物件。
**`Symbol.iterator`** 屬性的初始值與 {{jsxref("Array.prototype.values()", "values()")}} 屬性的初始值為相同的的函式物件。

## 語法

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ The `ArrayBuffer` constructor creates a new `ArrayBuffer` of the given length in

- `ArrayBuffer.length`
- : The `ArrayBuffer` constructor's length property whose value is 1.
- {{jsxref("ArrayBuffer.@@species", "get ArrayBuffer[@@species]")}}
- [`ArrayBuffer[Symbol.species]`](/zh-TW/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer/Symbol.species)
- : The constructor function that is used to create derived objects.
- {{jsxref("ArrayBuffer.prototype")}}
- : Allows the addition of properties to all `ArrayBuffer` objects.
Expand All @@ -57,8 +57,8 @@ The `ArrayBuffer` constructor creates a new `ArrayBuffer` of the given length in

### 屬性

- `ArrayBuffer.prototype[@@toStringTag]`
- : The initial value of the [`@@toStringTag`](/zh-TW/docs/Web/JavaScript/Reference/Global_Objects/Symbol/toStringTag) property is the string `"ArrayBuffer"`. This property is used in {{jsxref("Object.prototype.toString()")}}.
- `ArrayBuffer.prototype[Symbol.toStringTag]`
- : The initial value of the [`Symbol.toStringTag`](/zh-TW/docs/Web/JavaScript/Reference/Global_Objects/Symbol/toStringTag) property is the string `"ArrayBuffer"`. This property is used in {{jsxref("Object.prototype.toString()")}}.
- {{jsxref("ArrayBuffer.prototype.byteLength")}}
- : The read-only size, in bytes, of the `ArrayBuffer`. This is established when the array is constructed and cannot be changed.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ new Map([iterable])
- `Map.length`
- : `length`屬性的值為 0
要計算 `Map` 中有多少元素,可以使用 {{jsxref("Map.prototype.size")}}。
- {{jsxref("Map.@@species", "get Map[@@species]")}}
- [`Map[Symbol.species]`](/zh-TW/docs/Web/JavaScript/Reference/Global_Objects/Map/Symbol.species)
- : 用於創建派生物件的構造函數。
- {{jsxref("Map.prototype")}}
- : 表示 `Map` 構造函數的原型,允許對所有的 `Map` 物件添加屬性
Expand All @@ -52,8 +52,8 @@ new Map([iterable])

### 屬性

- `Map.prototype[@@toStringTag]`
- : The initial value of the [`@@toStringTag`](/zh-TW/docs/Web/JavaScript/Reference/Global_Objects/Symbol/toStringTag) property is the string `"Map"`. This property is used in {{jsxref("Object.prototype.toString()")}}.
- `Map.prototype[Symbol.toStringTag]`
- : The initial value of the [`Symbol.toStringTag`](/zh-TW/docs/Web/JavaScript/Reference/Global_Objects/Symbol/toStringTag) property is the string `"Map"`. This property is used in {{jsxref("Object.prototype.toString()")}}.
- {{jsxref("Map.prototype.size")}}
- : Returns the number of key/value pairs in the `Map` object.

Expand All @@ -71,7 +71,7 @@ new Map([iterable])
- : Returns a boolean indicating whether a value has been associated with the passed key in the `Map` object or not.
- {{jsxref("Map.prototype.set()")}}
- : Sets the value for the passed key in the `Map` object. Returns the `Map` object.
- {{jsxref("Map/@@iterator", "Map.prototype[@@iterator]()")}}
- [`Map.prototype[Symbol.iterator]()`](/zh-TW/docs/Web/JavaScript/Reference/Global_Objects/Map/Symbol.iterator)
- : Returns a new Iterator object that contains a two-member array of `[key, value]` for each element in the `Map` object in insertion order.
- {{jsxref("Map.prototype.keys()")}}
- : Returns a new Iterator object that contains the keys for each element in the `Map` object in insertion order.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ new Promise( /* executor */ function(resolve, reject) { ... } );

### 屬性

- `Promise.prototype[@@toStringTag]`
- : The initial value of the [`@@toStringTag`](/zh-TW/docs/Web/JavaScript/Reference/Global_Objects/Symbol/toStringTag) property is the string `"Promise"`. This property is used in {{jsxref("Object.prototype.toString()")}}.
- `Promise.prototype[Symbol.toStringTag]`
- : The initial value of the [`Symbol.toStringTag`](/zh-TW/docs/Web/JavaScript/Reference/Global_Objects/Symbol/toStringTag) property is the string `"Promise"`. This property is used in {{jsxref("Object.prototype.toString()")}}.

### 方法

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ new Set([iterable]);

## 描述

`Set` 對象是數值的收集器。你可以按插入順序迭代收集器中的元素。在 `Set` 裡的元素只會出現一次**;** 意即在`Set`裡的元素都是獨一無二
`Set` 對象是數值的收集器。你可以按插入順序迭代收集器中的元素。在 `Set` 裡的元素只會出現一次意即在 `Set` 裡的元素都是獨一無二

### 值的相等性

因為在 Set 裡每個值都是獨立的,所以都會檢查值的相等性。在早期的 ECMAScript 規範版本中,此處算法跟基於===操作符中使用的算法並不相同。具體來說,在 `Set`裡+0(在嚴格模式是和-0 相等)和-0 是不同的值。然而在 ECMAScript 2015 規範中這點已被更改。請參閱 [瀏覽器兼容性](#Browser_compatibility) 中的"Value equality for -0 and 0"
因為在 Set 裡每個值都是獨立的,所以都會檢查值的相等性。在早期的 ECMAScript 規範版本中,此處算法跟基於===操作符中使用的算法並不相同。具體來說,在 `Set` 裡 +0(在嚴格模式是和 -0 相等)和 -0 是不同的值。然而在 ECMAScript 2015 規範中這點已被更改。請參閱[瀏覽器相容性](#瀏覽器相容性)中的「Value equality for -0 and 0

另外,NaN 和 undefined 都可以被放置在 Set 中, NaN 之間被視為相同的值(儘管 NaN !== NaN)。

- `Set.length`
- : The value of the `length` property is 0.
- {{jsxref("Set.@@species", "get Set[@@species]")}}
- [`Set[Symbol.species]`](/zh-TW/docs/Web/JavaScript/Reference/Global_Objects/Set/Symbol.species)
- : The constructor function that is used to create derived objects.
- {{jsxref("Set.prototype")}}
- : Represents the prototype for the `Set` constructor. Allows the addition of properties to all `Set` objects.
Expand All @@ -47,8 +47,8 @@ All `Set` instances inherit from {{jsxref("Set.prototype")}}.

### 屬性

- `Set.prototype[@@toStringTag]`
- : The initial value of the [`@@toStringTag`](/zh-TW/docs/Web/JavaScript/Reference/Global_Objects/Symbol/toStringTag) property is the string `"Set"`. This property is used in {{jsxref("Object.prototype.toString()")}}.
- `Set.prototype[Symbol.toStringTag]`
- : The initial value of the [`Symbol.toStringTag`](/zh-TW/docs/Web/JavaScript/Reference/Global_Objects/Symbol/toStringTag) property is the string `"Set"`. This property is used in {{jsxref("Object.prototype.toString()")}}.
- {{jsxref("Set.prototype.size")}}
- : Returns the number of values in the `Set` object.

Expand All @@ -62,7 +62,7 @@ All `Set` instances inherit from {{jsxref("Set.prototype")}}.
- : Removes the element associated to the `value` and returns a boolean asserting whether an element was successfully removed or not. `Set.prototype.has(value)` will return `false` afterwards.
- {{jsxref("Set.prototype.has()")}}
- : Returns a boolean asserting whether an element is present with the given value in the `Set` object or not.
- {{jsxref("Set.prototype.@@iterator()", "Set.prototype[@@iterator]()")}}
- [`Set.prototype[Symbol.iterator]()`](/zh-TW/docs/Web/JavaScript/Reference/Global_Objects/Set/Symbol.iterator)
- : Returns a new iterator object that yields the **values** for each element in the `Set` object in insertion order.
- {{jsxref("Set.prototype.values()")}}
- : Returns a new iterator object that yields the **values** for each element in the `Set` object in insertion order.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ The following is a shim to provide support to non-supporting browsers:
- {{jsxref("String.prototype.valueOf()")}}
- : Returns the primitive value of the specified object. Overrides the
{{jsxref("Object.prototype.valueOf()")}} method.
- {{jsxref("String.prototype.@@iterator()", "String.prototype[@@iterator]()")}}
- [`String.prototype[Symbol.iterator]()`](/zh-TW/docs/Web/JavaScript/Reference/Global_Objects/String/Symbol.iterator)
- : Returns a new iterator object that iterates over the code points of a String value,
returning each code point as a String value.

Expand Down
39 changes: 19 additions & 20 deletions files/zh-tw/web/javascript/reference/iteration_protocols/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,22 @@ slug: Web/JavaScript/Reference/Iteration_protocols

為 ECMAScript 2015 中的一些補充內容,並非新的內建物件或語法,而是協議。這些協議可被任何遵守特定協定的物件所實作。

本文介紹兩種協議:[可迭代協議(iterable protocol)](#The_iterable_protocol)以及[迭代器協議(iterator protocol)](#The_iterator_protocol)。
本文介紹兩種協議:[可迭代協議](#可迭代協議)以及[迭代器協議](#迭代器協議)。

## 可迭代協議

**可迭代(iterable)**協議允許 JavaScript 物件定義或客制他們的迭代行為,例如哪些值可在 {{jsxref("Statements/for...of", "for..of")}} 語法結構中被迭代出來。部分內建型別為擁有預設迭代行為的[可迭代內建物件(built-in iterables)](#Built-in_iterables),如 {{jsxref("Array")}} 或 {{jsxref("Map")}},而其他型別(如 {{jsxref("Object")}})則否。
**可迭代**(iterable)協議允許 JavaScript 物件定義或客制他們的迭代行為,例如哪些值可在 {{jsxref("Statements/for...of", "for..of")}} 語法結構中被迭代出來。部分內建型別為擁有預設迭代行為的[可迭代內建物件](#可迭代內建物件),如 {{jsxref("Array")}} 或 {{jsxref("Map")}},而其他型別(如 {{jsxref("Object")}})則否。

為了成為**可迭代的(iterable)**,一個物件必須實作 **@@iterator** 方法,意思是這個物件(或其[原型鏈](/zh-TW/docs/Web/JavaScript/Guide/Inheritance_and_the_prototype_chain)中的其中一個原型物件)必須擁有一個鍵(key)值為 **@@iterator**(即 {{jsxref("Symbol.iterator")}} 常數)的屬性:
為了成為**可迭代的**,一個物件必須實作 **`[Symbol.iterator]()`** 方法,意思是這個物件(或其[原型鏈](/zh-TW/docs/Web/JavaScript/Inheritance_and_the_prototype_chain)中的其中一個原型物件)必須擁有一個鍵(key)值為 `[Symbol.iterator]`(即 {{jsxref("Symbol.iterator")}} 常數)的屬性:

| 屬性 | 值 |
| ------------------- | ------------------------------------------------------------------------------------- |
| `[Symbol.iterator]` | 回傳符合[迭代器協議(iterator protocol)](#The_iterator_protocol)之物件的無引數函式。 |
- `[Symbol.iterator]`
- : 回傳符合[迭代器協議](#迭代器協議)之物件的無引數函式。

每當物件需要被迭代時(比如在一個開始的 `for..of` 迴圈中),物件的 `@@iterator` 方法會被以不傳入引數的方式呼叫,並會使用其回傳的**迭代器(iterator)**來獲得被迭代出來的值。
每當物件需要被迭代時(比如在一個開始的 `for..of` 迴圈中),物件的 `[Symbol.iterator]()` 方法會被以不傳入引數的方式呼叫,並會使用其回傳的**迭代器**來獲得被迭代出來的值。

## 迭代器協議

**迭代器(iterator)**協議定義了一個標準方式來產出一連串(有限或無限)的值,並且可能於所有值都被產出後回傳一個值。
**迭代器**(iterator)協議定義了一個標準方式來產出一連串(有限或無限)的值,並且可能於所有值都被產出後回傳一個值。

當物件以下列語義實作了 **`next()`** 方法即為一個迭代器:

Expand Down Expand Up @@ -90,7 +89,7 @@ var someString = "hi";
typeof someString[Symbol.iterator]; // "function"
```

`String` 的[預設迭代器](/zh-TW/docs/Web/JavaScript/Reference/Global_Objects/String/@@iterator)會回傳字串中的一個一個字元:
`String` 的[預設迭代器](/zh-TW/docs/Web/JavaScript/Reference/Global_Objects/String/Symbol.iterator)會回傳字串中的一個一個字元:

```js
var iterator = someString[Symbol.iterator]();
Expand All @@ -101,13 +100,13 @@ iterator.next(); // { value: "i", done: false }
iterator.next(); // { value: undefined, done: true }
```

部分內建語法結構(built-in constructs),如 [spread syntax](/zh-TW/docs/Web/JavaScript/Reference/Operators/Spread_operator),其內部也使用了相同的迭代協議:
部分內建語法結構(built-in constructs),如 [spread syntax](/zh-TW/docs/Web/JavaScript/Reference/Operators/Spread_syntax),其內部也使用了相同的迭代協議:

```js
[...someString]; // ["h", "i"]
```

我們可以藉由提供我們自己的 `@@iterator` 來重新定義迭代行為:
我們可以藉由提供我們自己的 `[Symbol.iterator]()` 來重新定義迭代行為:

```js
var someString = new String("hi"); // need to construct a String object explicitly to avoid auto-boxing
Expand All @@ -128,7 +127,7 @@ someString[Symbol.iterator] = function () {
};
```

請注意,重新定義 `@@iterator` 會影響使用迭代協議之內建語法結構的行為:
請注意,重新定義 `[Symbol.iterator]()` 會影響使用迭代協議之內建語法結構的行為:

```js
[...someString]; // ["bye"]
Expand All @@ -139,7 +138,7 @@ someString + ""; // "hi"

### 可迭代內建物件

{{jsxref("String")}}、{{jsxref("Array")}}、{{jsxref("TypedArray")}}、{{jsxref("Map")}} 以及 {{jsxref("Set")}} 全都是可迭代內建物件,因為他們每一個的原型物件皆實作了 `@@iterator` 方法。
{{jsxref("String")}}、{{jsxref("Array")}}、{{jsxref("TypedArray")}}、{{jsxref("Map")}} 以及 {{jsxref("Set")}} 全都是可迭代內建物件,因為他們每一個的原型物件皆實作了 `[Symbol.iterator]()` 方法。

### 自定義可迭代物件

Expand Down Expand Up @@ -186,7 +185,7 @@ new WeakSet(

### 用於可迭代物件的語法

部分陳述式(statements)及運算式(expressions)為預期用於可迭代物件,例如 [`for-of`](/zh-TW/docs/Web/JavaScript/Reference/Statements/for...of) 迴圈、[spread syntax](/zh-TW/docs/Web/JavaScript/Reference/Operators/Spread_operator)、[`yield*`](/zh-TW/docs/Web/JavaScript/Reference/Operators/yield*),及[解構賦值](/zh-TW/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment):
部分陳述式(statements)及運算式(expressions)為預期用於可迭代物件,例如 [`for-of`](/zh-TW/docs/Web/JavaScript/Reference/Statements/for...of) 迴圈、[spread syntax](/zh-TW/docs/Web/JavaScript/Reference/Operators/Spread_syntax)、[`yield*`](/zh-TW/docs/Web/JavaScript/Reference/Operators/yield*),及[解構賦值](/zh-TW/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment):

```js
for (let value of ["a", "b", "c"]) {
Expand All @@ -208,9 +207,9 @@ gen().next(); // { value:"a", done:false }
a; // "a"
```

### 非良好的(Non-well-formed)可迭代物件
### 非良好的可迭代物件

假如可迭件物件的 `@@iterator` 方法不是回傳一個迭代器物件,即是非良好的(non-well-formed)可迭代物件。如以下方式使用可能會導致執行時期異常或錯誤行為:
假如可迭件物件的 `[Symbol.iterator]()` 方法不是回傳一個迭代器物件,即是非良好的(non-well-formed)可迭代物件。如以下方式使用可能會導致執行時期異常或錯誤行為:

```js
var nonWellFormedIterable = {}
Expand Down Expand Up @@ -325,7 +324,7 @@ for (const val of simple) {

## 生成器物件是迭代器還是可迭代物件?

[生成器物件(generator object)](/zh-TW/docs/Web/JavaScript/Reference/Global_Objects/Generator)同時為迭代器及可迭代物件:
[生成器物件](/zh-TW/docs/Web/JavaScript/Reference/Global_Objects/Generator)同時為迭代器及可迭代物件:

```js
var aGeneratorObject = (function* () {
Expand All @@ -336,13 +335,13 @@ var aGeneratorObject = (function* () {
typeof aGeneratorObject.next;
// "function", because it has a next method, so it's an iterator
typeof aGeneratorObject[Symbol.iterator];
// "function", because it has an @@iterator method, so it's an iterable
// "function", because it has an [Symbol.iterator]() method, so it's an iterable
aGeneratorObject[Symbol.iterator]() === aGeneratorObject;
// true, because its @@iterator method returns itself (an iterator), so it's an well-formed iterable
// true, because its [Symbol.iterator]() method returns itself (an iterator), so it's an well-formed iterable
[...aGeneratorObject];
// [1, 2, 3]
```

## 參見

- 更多關於 ES2015 生成器(generators)的資訊,可參考[生成器函式 function\* 文件](/zh-TW/docs/Web/JavaScript/Reference/Statements/function*)。
- 更多關於 ES2015 生成器(generator)的資訊,可參考[生成器函式 function\* 文件](/zh-TW/docs/Web/JavaScript/Reference/Statements/function*)。
Loading