From f85112ba38001b1d115c801bba265f5bdb09c0cc Mon Sep 17 00:00:00 2001 From: mdn-bot <108879845+mdn-bot@users.noreply.github.com> Date: Tue, 16 Jul 2024 01:29:37 +0000 Subject: [PATCH 1/2] zh-tw: sync translated content --- files/zh-tw/_redirects.txt | 1 + files/zh-tw/_wikihistory.json | 2 +- .../array/{@@iterator => symbol.iterator}/index.md | 3 ++- 3 files changed, 4 insertions(+), 2 deletions(-) rename files/zh-tw/web/javascript/reference/global_objects/array/{@@iterator => symbol.iterator}/index.md (90%) diff --git a/files/zh-tw/_redirects.txt b/files/zh-tw/_redirects.txt index 0f52fe5a76a24a..287e64a6387cc6 100644 --- a/files/zh-tw/_redirects.txt +++ b/files/zh-tw/_redirects.txt @@ -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 diff --git a/files/zh-tw/_wikihistory.json b/files/zh-tw/_wikihistory.json index b16c911d881831..fe6248412a9e56 100644 --- a/files/zh-tw/_wikihistory.json +++ b/files/zh-tw/_wikihistory.json @@ -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"] }, diff --git a/files/zh-tw/web/javascript/reference/global_objects/array/@@iterator/index.md b/files/zh-tw/web/javascript/reference/global_objects/array/symbol.iterator/index.md similarity index 90% rename from files/zh-tw/web/javascript/reference/global_objects/array/@@iterator/index.md rename to files/zh-tw/web/javascript/reference/global_objects/array/symbol.iterator/index.md index 275a5229b0e971..36f4317bb1b645 100644 --- a/files/zh-tw/web/javascript/reference/global_objects/array/@@iterator/index.md +++ b/files/zh-tw/web/javascript/reference/global_objects/array/symbol.iterator/index.md @@ -1,6 +1,7 @@ --- title: Array.prototype[@@iterator]() -slug: Web/JavaScript/Reference/Global_Objects/Array/@@iterator +slug: Web/JavaScript/Reference/Global_Objects/Array/Symbol.iterator +original_slug: Web/JavaScript/Reference/Global_Objects/Array/@@iterator --- {{JSRef}} From 4ae0a9cb4990461afc93cefffd13088d6152e37c Mon Sep 17 00:00:00 2001 From: allo Date: Tue, 16 Jul 2024 13:23:35 +0800 Subject: [PATCH 2/2] Remove all `@@notation` from page slugs ref: mdn/content#34824 --- .../reference/global_objects/array/index.md | 6 +-- .../array/symbol.iterator/index.md | 5 +-- .../global_objects/arraybuffer/index.md | 6 +-- .../reference/global_objects/map/index.md | 8 ++-- .../reference/global_objects/promise/index.md | 4 +- .../reference/global_objects/set/index.md | 12 +++--- .../reference/global_objects/string/index.md | 2 +- .../reference/iteration_protocols/index.md | 39 +++++++++---------- .../reference/statements/for...of/index.md | 12 +++--- 9 files changed, 46 insertions(+), 48 deletions(-) diff --git a/files/zh-tw/web/javascript/reference/global_objects/array/index.md b/files/zh-tw/web/javascript/reference/global_objects/array/index.md index 2db87ddc815e88..2cf5fe2b9b5092 100644 --- a/files/zh-tw/web/javascript/reference/global_objects/array/index.md +++ b/files/zh-tw/web/javascript/reference/global_objects/array/index.md @@ -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")}} - : 可加入屬性至所有陣列物件。 @@ -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. ### 方法 @@ -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. ## 範例 diff --git a/files/zh-tw/web/javascript/reference/global_objects/array/symbol.iterator/index.md b/files/zh-tw/web/javascript/reference/global_objects/array/symbol.iterator/index.md index 36f4317bb1b645..855a3c5345ff11 100644 --- a/files/zh-tw/web/javascript/reference/global_objects/array/symbol.iterator/index.md +++ b/files/zh-tw/web/javascript/reference/global_objects/array/symbol.iterator/index.md @@ -1,12 +1,11 @@ --- -title: Array.prototype[@@iterator]() +title: Array.prototype[Symbol.iterator]() slug: Web/JavaScript/Reference/Global_Objects/Array/Symbol.iterator -original_slug: Web/JavaScript/Reference/Global_Objects/Array/@@iterator --- {{JSRef}} -**`@@iterator`** 屬性的初始值與 {{jsxref("Array.prototype.values()", "values()")}} 屬性的初始值為相同的的函式物件。 +**`Symbol.iterator`** 屬性的初始值與 {{jsxref("Array.prototype.values()", "values()")}} 屬性的初始值為相同的的函式物件。 ## 語法 diff --git a/files/zh-tw/web/javascript/reference/global_objects/arraybuffer/index.md b/files/zh-tw/web/javascript/reference/global_objects/arraybuffer/index.md index c84e51573a638e..2b400bc7e7d7f8 100644 --- a/files/zh-tw/web/javascript/reference/global_objects/arraybuffer/index.md +++ b/files/zh-tw/web/javascript/reference/global_objects/arraybuffer/index.md @@ -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. @@ -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. diff --git a/files/zh-tw/web/javascript/reference/global_objects/map/index.md b/files/zh-tw/web/javascript/reference/global_objects/map/index.md index 69dc28a62eb6cd..a21afbe591761e 100644 --- a/files/zh-tw/web/javascript/reference/global_objects/map/index.md +++ b/files/zh-tw/web/javascript/reference/global_objects/map/index.md @@ -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` 物件添加屬性 @@ -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. @@ -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. diff --git a/files/zh-tw/web/javascript/reference/global_objects/promise/index.md b/files/zh-tw/web/javascript/reference/global_objects/promise/index.md index 82692b59683eff..a8bf34a3ebfa46 100644 --- a/files/zh-tw/web/javascript/reference/global_objects/promise/index.md +++ b/files/zh-tw/web/javascript/reference/global_objects/promise/index.md @@ -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()")}}. ### 方法 diff --git a/files/zh-tw/web/javascript/reference/global_objects/set/index.md b/files/zh-tw/web/javascript/reference/global_objects/set/index.md index 596ff71582b15a..d1a0fcca7302cf 100644 --- a/files/zh-tw/web/javascript/reference/global_objects/set/index.md +++ b/files/zh-tw/web/javascript/reference/global_objects/set/index.md @@ -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. @@ -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. @@ -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. diff --git a/files/zh-tw/web/javascript/reference/global_objects/string/index.md b/files/zh-tw/web/javascript/reference/global_objects/string/index.md index 437709397bb913..b9f538b89eabbb 100644 --- a/files/zh-tw/web/javascript/reference/global_objects/string/index.md +++ b/files/zh-tw/web/javascript/reference/global_objects/string/index.md @@ -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. diff --git a/files/zh-tw/web/javascript/reference/iteration_protocols/index.md b/files/zh-tw/web/javascript/reference/iteration_protocols/index.md index 55d91e3c82bc96..87b8eb9443d661 100644 --- a/files/zh-tw/web/javascript/reference/iteration_protocols/index.md +++ b/files/zh-tw/web/javascript/reference/iteration_protocols/index.md @@ -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()`** 方法即為一個迭代器: @@ -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](); @@ -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 @@ -128,7 +127,7 @@ someString[Symbol.iterator] = function () { }; ``` -請注意,重新定義 `@@iterator` 會影響使用迭代協議之內建語法結構的行為: +請注意,重新定義 `[Symbol.iterator]()` 會影響使用迭代協議之內建語法結構的行為: ```js [...someString]; // ["bye"] @@ -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]()` 方法。 ### 自定義可迭代物件 @@ -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"]) { @@ -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 = {} @@ -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* () { @@ -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*)。 diff --git a/files/zh-tw/web/javascript/reference/statements/for...of/index.md b/files/zh-tw/web/javascript/reference/statements/for...of/index.md index 9cab619760aa2f..366b3363340278 100644 --- a/files/zh-tw/web/javascript/reference/statements/for...of/index.md +++ b/files/zh-tw/web/javascript/reference/statements/for...of/index.md @@ -29,7 +29,7 @@ for (variable of iterable) `for...of` 迴圈依序逐個操作來自可迭代物件的值。迴圈對值的每次操作稱為一次*迭代*,而迴圈本身則稱為*迭代可迭代物件*,每次迭代執行可能參考當前序列值的語句。 -當 `for...of` 迴圈在可迭代物件上進行迭代時,它首先調用可迭代物件的 [`[@@iterator]()`](/zh-TW/docs/Web/JavaScript/Reference/Global_Objects/Symbol/iterator) 方法,該方法回傳一個[迭代器](/zh-TW/docs/Web/JavaScript/Reference/Iteration_protocols#迭代器協議),然後重複調用得到的迭代器的 [`next()`](/zh-TW/docs/Web/JavaScript/Reference/Iteration_protocols#迭代器協議) 方法,以生成要賦予 `variable` 的值序列。 +當 `for...of` 迴圈在可迭代物件上進行迭代時,它首先調用可迭代物件的 [`[Symbol.iterator]()`](/zh-TW/docs/Web/JavaScript/Reference/Global_Objects/Symbol/iterator) 方法,該方法回傳一個[迭代器](/zh-TW/docs/Web/JavaScript/Reference/Iteration_protocols#迭代器協議),然後重複調用得到的迭代器的 [`next()`](/zh-TW/docs/Web/JavaScript/Reference/Iteration_protocols#迭代器協議) 方法,以生成要賦予 `variable` 的值序列。 `for...of` 迴圈在迭代器完成時退出(即迭代器的 `next()` 方法回傳一個包含 `done: true` 的物件)。你也可以使用流程控制語句來改變正常的控制流程。[`break`](/zh-TW/docs/Web/JavaScript/Reference/Statements/break) 會退出迴圈並轉到迴圈區塊後的第一個語句,而 [`continue`](/zh-TW/docs/Web/JavaScript/Reference/Statements/continue) 會跳過當前迭代的其餘語句並進行下一次迭代。 @@ -79,7 +79,7 @@ for (const value of iterable) { ### 迭代字串 -字串將依 [Unicode 編碼位置](/zh-TW/docs/Web/JavaScript/Reference/Global_Objects/String/@@iterator)迭代。 +字串將依 [Unicode 編碼位置](/zh-TW/docs/Web/JavaScript/Reference/Global_Objects/String/Symbol.iterator)迭代。 ```js const iterable = "boo"; @@ -171,7 +171,7 @@ for (const paragraph of articleParagraphs) { ### 迭代用戶定義的可迭代物件 -迭代帶有回傳自訂迭代器的 `@@iterator` 方法的物件: +迭代帶有回傳自訂迭代器的 `[Symbol.iterator]()` 方法的物件: ```js const iterable = { @@ -196,7 +196,7 @@ for (const value of iterable) { // 3 ``` -迭代帶有 `@@iterator` 生成器方法的物件: +迭代帶有 `[Symbol.iterator]()` 生成器方法的物件: ```js const iterable = { @@ -215,7 +215,7 @@ for (const value of iterable) { // 3 ``` -_可迭代迭代器_(帶有回傳 `this` 的 `[@@iterator]()` 方法的迭代器)是一種相當常見的技術,用來使迭代器在期望可迭代物件的語法中使用,例如 `for...of`。 +_可迭代迭代器_(帶有回傳 `this` 的 `[Symbol.iterator]()` 方法的迭代器)是一種相當常見的技術,用來使迭代器在期望可迭代物件的語法中使用,例如 `for...of`。 ```js let i = 1; @@ -357,7 +357,7 @@ for (const i of iterable) { 第二個迴圈與第一個迴圈類似,但它使用 {{jsxref("Object.hasOwn()")}} 來檢查找到的可枚舉屬性是否為物件的自有屬性,即非繼承屬性。如果是,則輸出該屬性。屬性 `0`、`1`、`2` 和 `foo` 皆被輸出,因為它們是自有屬性。屬性 `arrCustom` 和 `objCustom` 都沒有被輸出,因為它們是繼承屬性。 -`for...of` 迴圈迭代並輸出 `iterable` 按照[可迭代](/zh-TW/docs/Web/JavaScript/Reference/Global_Objects/Array/@@iterator)陣列定義要進行迭代的*值*。物件的*元素* `3`、`5`、`7` 被輸出,但物件的*屬性*沒有被輸出。 +`for...of` 迴圈迭代並輸出 `iterable` 按照[可迭代](/zh-TW/docs/Web/JavaScript/Reference/Global_Objects/Array/Symbol.iterator)陣列定義要進行迭代的*值*。物件的*元素* `3`、`5`、`7` 被輸出,但物件的*屬性*沒有被輸出。 ## 規範