diff options
Diffstat (limited to 'files/zh-tw/web/javascript/reference/global_objects/set')
7 files changed, 752 insertions, 0 deletions
diff --git a/files/zh-tw/web/javascript/reference/global_objects/set/add/index.html b/files/zh-tw/web/javascript/reference/global_objects/set/add/index.html new file mode 100644 index 0000000000..b0a85a0a06 --- /dev/null +++ b/files/zh-tw/web/javascript/reference/global_objects/set/add/index.html @@ -0,0 +1,83 @@ +--- +title: Set.prototype.add() +slug: Web/JavaScript/Reference/Global_Objects/Set/add +tags: + - ECMAScript 2015 + - JavaScript + - 原型 + - 參考 + - 方法 + - 集合 +translation_of: Web/JavaScript/Reference/Global_Objects/Set/add +--- +<div>{{JSRef}}</div> + +<p><code><strong>add()</strong></code> 會在一個 <code>Set</code> 物件的尾端加上一個指定 <code>value</code> 的新元素。</p> + +<div>{{EmbedInteractiveExample("pages/js/set-prototype-add.html")}}</div> + + + +<h2 id="語法">語法</h2> + +<pre class="syntaxbox"><em>mySet</em>.add(<em>value</em>);</pre> + +<h3 id="參數">參數</h3> + +<dl> + <dt><code>value</code></dt> + <dd>要被加到 <code>Set</code> 物件中的值。</dd> +</dl> + +<h3 id="回傳值">回傳值</h3> + +<p><code>Set</code> 物件本身。</p> + +<h2 id="範例">範例</h2> + +<h3 id="使用_add_方法">使用 <code>add</code> 方法</h3> + +<pre class="brush: js">var mySet = new Set(); + +mySet.add(1); +mySet.add(5).add('some text'); // chainable + +console.log(mySet); +// Set [1, 5, "some text"] +</pre> + +<h2 id="規範">規範</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">規範</th> + <th scope="col">狀態</th> + <th scope="col">註</th> + </tr> + <tr> + <td>{{SpecName('ES2015', '#sec-set.prototype.add', 'Set.prototype.add')}}</td> + <td>{{Spec2('ES2015')}}</td> + <td>Initial definition.</td> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-set.prototype.add', 'Set.prototype.add')}}</td> + <td>{{Spec2('ESDraft')}}</td> + <td> </td> + </tr> + </tbody> +</table> + +<h2 id="瀏覽器相容性">瀏覽器相容性</h2> + + + +<p>{{Compat("javascript.builtins.Set.add")}}</p> + +<h2 id="另見">另見</h2> + +<ul> + <li>{{jsxref("Set")}}</li> + <li>{{jsxref("Set.prototype.delete()")}}</li> + <li>{{jsxref("Set.prototype.has()")}}</li> +</ul> diff --git a/files/zh-tw/web/javascript/reference/global_objects/set/clear/index.html b/files/zh-tw/web/javascript/reference/global_objects/set/clear/index.html new file mode 100644 index 0000000000..2eb8875c34 --- /dev/null +++ b/files/zh-tw/web/javascript/reference/global_objects/set/clear/index.html @@ -0,0 +1,79 @@ +--- +title: Set.prototype.clear() +slug: Web/JavaScript/Reference/Global_Objects/Set/clear +tags: + - ECMAScript 2015 + - JavaScript + - 原型 + - 參考 + - 方法 + - 集合 +translation_of: Web/JavaScript/Reference/Global_Objects/Set/clear +--- +<div>{{JSRef}}</div> + +<p><code><strong>clear()</strong></code> 方法會從一個 <code>Set</code> 物件中移除其所有元素。</p> + +<div>{{EmbedInteractiveExample("pages/js/set-prototype-clear.html")}}</div> + + + +<h2 id="語法">語法</h2> + +<pre class="syntaxbox"><em>mySet</em>.clear();</pre> + +<h3 id="回傳值">回傳值</h3> + +<p>{{jsxref("undefined")}}.</p> + +<h2 id="範例">範例</h2> + +<h3 id="使用_clear_方法">使用 <code>clear</code> 方法</h3> + +<pre class="brush: js">var mySet = new Set(); +mySet.add(1); +mySet.add('foo'); + +mySet.size; // 2 +mySet.has('foo'); // true + +mySet.clear(); + +mySet.size; // 0 +mySet.has('bar') // false +</pre> + +<h2 id="規範">規範</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">規範</th> + <th scope="col">狀態</th> + <th scope="col">註</th> + </tr> + <tr> + <td>{{SpecName('ES2015', '#sec-set.prototype.clear', 'Set.prototype.clear')}}</td> + <td>{{Spec2('ES2015')}}</td> + <td>Initial definition.</td> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-set.prototype.clear', 'Set.prototype.clear')}}</td> + <td>{{Spec2('ESDraft')}}</td> + <td> </td> + </tr> + </tbody> +</table> + +<h2 id="瀏覽器相容性">瀏覽器相容性</h2> + + + +<p>{{Compat("javascript.builtins.Set.clear")}}</p> + +<h2 id="另見">另見</h2> + +<ul> + <li>{{jsxref("Set")}}</li> + <li>{{jsxref("Set.prototype.delete()")}}</li> +</ul> diff --git a/files/zh-tw/web/javascript/reference/global_objects/set/delete/index.html b/files/zh-tw/web/javascript/reference/global_objects/set/delete/index.html new file mode 100644 index 0000000000..d465f45bef --- /dev/null +++ b/files/zh-tw/web/javascript/reference/global_objects/set/delete/index.html @@ -0,0 +1,98 @@ +--- +title: Set.prototype.delete() +slug: Web/JavaScript/Reference/Global_Objects/Set/delete +tags: + - ECMAScript 2015 + - JavaScript + - 原型 + - 參考 + - 方法 + - 集合 +translation_of: Web/JavaScript/Reference/Global_Objects/Set/delete +--- +<div>{{JSRef}}</div> + +<p><code><strong>delete()</strong></code> 方法會一個 <code>Set</code> 物件中移除指定元素。</p> + +<div>{{EmbedInteractiveExample("pages/js/set-prototype-delete.html")}}</div> + + + +<h2 id="語法">語法</h2> + +<pre class="syntaxbox"><em>mySet</em>.delete(<em>value</em>);</pre> + +<h3 id="參數'">參數'</h3> + +<dl> + <dt><code>value</code></dt> + <dd>要從 <code>Set</code> 物件中移除的值。</dd> +</dl> + +<h3 id="回傳值">回傳值</h3> + +<p><code>true</code> 如果成功從 <code>Set</code> 物件中移除;反之 <code>false</code>。</p> + +<h2 id="範例">範例</h2> + +<h3 id="使用_delete_方法">使用 <code>delete</code> 方法</h3> + +<pre class="brush: js">var mySet = new Set(); +mySet.add('foo'); + +mySet.delete('bar'); // Returns false. No "bar" element found to be deleted. +mySet.delete('foo'); // Returns true. Successfully removed. + +mySet.has('foo'); // Returns false. The "foo" element is no longer present. +</pre> + +<p>下方展示了如何從一個 Set 中移除物件。</p> + +<pre class="brush: js">var setObj = new Set(); // Create a New Set. + +setObj.add({x: 10, y: 20}); // Add object in the set. + +setObj.add({x: 20, y: 30}); // Add object in the set. + +// Delete any point with `x > 10`. +setObj.forEach(function(point){ + if(point.x > 10){ + setObj.delete(point) + } +}) +</pre> + +<h2 id="規範">規範</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">規範</th> + <th scope="col">狀態</th> + <th scope="col">註</th> + </tr> + <tr> + <td>{{SpecName('ES2015', '#sec-set.prototype.delete', 'Set.prototype.delete')}}</td> + <td>{{Spec2('ES2015')}}</td> + <td>Initial definition.</td> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-set.prototype.delete', 'Set.prototype.delete')}}</td> + <td>{{Spec2('ESDraft')}}</td> + <td> </td> + </tr> + </tbody> +</table> + +<h2 id="瀏覽器相容性">瀏覽器相容性</h2> + + + +<p>{{Compat("javascript.builtins.Set.delete")}}</p> + +<h2 id="另見">另見</h2> + +<ul> + <li>{{jsxref("Set")}}</li> + <li>{{jsxref("Set.prototype.clear()")}}</li> +</ul> diff --git a/files/zh-tw/web/javascript/reference/global_objects/set/entries/index.html b/files/zh-tw/web/javascript/reference/global_objects/set/entries/index.html new file mode 100644 index 0000000000..6bf609afb2 --- /dev/null +++ b/files/zh-tw/web/javascript/reference/global_objects/set/entries/index.html @@ -0,0 +1,78 @@ +--- +title: Set.prototype.entries() +slug: Web/JavaScript/Reference/Global_Objects/Set/entries +tags: + - ECMAScript 2015 + - JavaScript + - 原型 + - 方法 + - 迭代器 + - 集合 +translation_of: Web/JavaScript/Reference/Global_Objects/Set/entries +--- +<div>{{JSRef}}</div> + +<div><code><strong>entries()</strong></code> 方法回傳一個 <code>Iterator</code> 物件,其包含著一個由插入順序排序,<code>Set</code> 物件中每個元素的<strong> <code>[value, value]</code></strong> 陣列。儘管對 <code>Set</code> 物件來說沒有像 <code>Map</code> 一樣的 <code>key</code> 概念,為了確保這個 API 運作的與 <code>Map</code> 相似,每個 <em>entry</em> 都有同樣的值同時作為其 <em>key</em> 和 <em>value</em> ,因此回傳的是一個<strong><code>[value, value]</code></strong> 的陣列。</div> + +<div>{{EmbedInteractiveExample("pages/js/set-prototype-entries.html")}}</div> + + + +<h2 id="語法">語法</h2> + +<pre class="syntaxbox"><code><em>mySet</em>.entries()</code></pre> + +<h3 id="回傳值">回傳值</h3> + +<p>一個新的 <code>Iterator</code> 物件,包含著一個由插入順序排序,<code>Set</code> 物件中每個元素的<strong> <code>[value, value]</code></strong> 陣列。</p> + +<h2 id="範例">範例</h2> + +<h3 id="使用_entries()">使用 <code>entries()</code></h3> + +<pre class="brush:js">var mySet = new Set(); +mySet.add('foobar'); +mySet.add(1); +mySet.add('baz'); + +var setIter = mySet.entries(); + +console.log(setIter.next().value); // ["foobar", "foobar"] +console.log(setIter.next().value); // [1, 1] +console.log(setIter.next().value); // ["baz", "baz"] +</pre> + +<h2 id="規範">規範</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">規範</th> + <th scope="col">狀態</th> + <th scope="col">註</th> + </tr> + <tr> + <td>{{SpecName('ES2015', '#sec-set.prototype.entries', 'Set.prototype.entries')}}</td> + <td>{{Spec2('ES2015')}}</td> + <td>Initial definition.</td> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-set.prototype.entries', 'Set.prototype.entries')}}</td> + <td>{{Spec2('ESDraft')}}</td> + <td> </td> + </tr> + </tbody> +</table> + +<h2 id="瀏覽器相容性">瀏覽器相容性</h2> + + + +<p>{{Compat("javascript.builtins.Set.entries")}}</p> + +<h2 id="另見">另見</h2> + +<ul> + <li>{{jsxref("Set.prototype.keys()")}}</li> + <li>{{jsxref("Set.prototype.values()")}}</li> +</ul> diff --git a/files/zh-tw/web/javascript/reference/global_objects/set/has/index.html b/files/zh-tw/web/javascript/reference/global_objects/set/has/index.html new file mode 100644 index 0000000000..c77d2ea99b --- /dev/null +++ b/files/zh-tw/web/javascript/reference/global_objects/set/has/index.html @@ -0,0 +1,92 @@ +--- +title: Set.prototype.has() +slug: Web/JavaScript/Reference/Global_Objects/Set/has +tags: + - ECMAScript 2015 + - JavaScript + - 原型 + - 方法 + - 集合 +translation_of: Web/JavaScript/Reference/Global_Objects/Set/has +--- +<div>{{JSRef}}</div> + +<p><code><strong>has()</strong></code> 方法對一個指定值元素在 <code>Set</code> 物件中的存在與否回傳一個布林值。</p> + +<div>{{EmbedInteractiveExample("pages/js/set-prototype-has.html")}}</div> + + + +<h2 id="語法">語法</h2> + +<pre class="syntaxbox"><em>mySet</em>.has(<em>value</em>);</pre> + +<h3 id="參數">參數</h3> + +<dl> + <dt><code>value</code></dt> + <dd>要測試是否存在在 <code>Set</code> 中的值。</dd> +</dl> + +<h3 id="回傳值">回傳值</h3> + +<p>回傳 <code>true</code> 如果給定值存在在 <code>Set</code> 物件中;反之回傳 <code>false</code> 。</p> + +<div class="blockIndicator note"> +<p><strong>Note:</strong> 技術上來說,<code>has()</code> 使用了 <code><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness#Same-value-zero_equality">sameValueZero</a></code> 算法來判斷給定元素的存在與否。</p> +</div> + +<h2 id="範例">範例</h2> + +<h3 id="使用_has_方法">使用 <code>has</code> 方法</h3> + +<pre class="brush: js">var mySet = new Set(); +mySet.add('foo'); + +mySet.has('foo'); // returns true +mySet.has('bar'); // returns false + +var set1 = new Set(); +var obj1 = {'key1': 1}; +set1.add(obj1); + +set1.has(obj1); // returns true +set1.has({'key1': 1}); // returns false because they are different object references +set1.add({'key1': 1}); // now set1 contains 2 entries +</pre> + +<h2 id="規範">規範</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">規範</th> + <th scope="col">狀態</th> + <th scope="col">註</th> + </tr> + <tr> + <td>{{SpecName('ES2015', '#sec-set.prototype.has', 'Set.prototype.has')}}</td> + <td>{{Spec2('ES2015')}}</td> + <td>Initial definition.</td> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-set.prototype.has', 'Set.prototype.has')}}</td> + <td>{{Spec2('ESDraft')}}</td> + <td> </td> + </tr> + </tbody> +</table> + +<h2 id="瀏覽器相容性">瀏覽器相容性</h2> + + + +<p>{{Compat("javascript.builtins.Set.has")}}</p> + +<h2 id="另見">另見</h2> + +<ul> + <li>{{jsxref("Set")}}</li> + <li>{{jsxref("Set.prototype.add()")}}</li> + <li>{{jsxref("Set.prototype.delete()")}}</li> +</ul> diff --git a/files/zh-tw/web/javascript/reference/global_objects/set/index.html b/files/zh-tw/web/javascript/reference/global_objects/set/index.html new file mode 100644 index 0000000000..2b3f80fdd1 --- /dev/null +++ b/files/zh-tw/web/javascript/reference/global_objects/set/index.html @@ -0,0 +1,243 @@ +--- +title: Set +slug: Web/JavaScript/Reference/Global_Objects/Set +tags: + - ECMAScript 2015 + - Global Objects + - JavaScript + - Object + - set +translation_of: Web/JavaScript/Reference/Global_Objects/Set +--- +<div>{{JSRef}}</div> + +<p><strong><code>Set</code></strong> 物件可讓你儲存任何類型的唯一值(unique),不論是{{Glossary("Primitive", "基本型別(primitive)值")}}或物件參考(references)。</p> + +<div>{{EmbedInteractiveExample("pages/js/set-prototype-constructor.html")}}</div> + + + +<h2 id="語法">語法</h2> + +<pre class="syntaxbox notranslate">new Set([<em>iterable</em>]);</pre> + +<h3 id="參數">參數</h3> + +<dl> + <dt><code>iterable</code></dt> + <dd>若一個<a href="/zh-TW/docs/Web/JavaScript/Reference/Statements/for...of">可迭代物件</a>被傳入,其所有的元素將會被加入至新的 <code>Set</code>。若你沒有指定此參數,或參數值為 <code>null</code>,則新的 <code>Set</code> 會是空的。</dd> +</dl> + +<h3 id="回傳值">回傳值</h3> + +<p>一個新的 <code>Set</code> 物件。</p> + +<h2 id="描述">描述</h2> + +<p><code>Set</code> 對象是數值的收集器。您可以按插入順序迭代收集器中的元素。在 <code>Set</code> 裡的元素只會出現一次<strong>;</strong> 意即在<code>Set</code>裡的元素都是獨一無二</p> + +<h3 id="值的相等性">值的相等性</h3> + +<p>因為在 Set 裡每個值都是獨立的,所以都會檢查值的相等性。在早期的ECMAScript規範版本中,此處算法跟基於===操作符中使用的算法並不相同。具體來說,在 <code>Set</code>裡+0(在嚴格模式是和-0相等)和-0是不同的值。然而在 ECMAScript 2015規範中這點已被更改。請參閱 <a href="#Browser_compatibility">瀏覽器兼容性</a> 中的"Value equality for -0 and 0"。</p> + +<p>另外,NaN和undefined都可以被放置在Set 中, NaN之間被視為相同的值(儘管 NaN !== NaN)。</p> + +<dl> + <dt><code>Set.length</code></dt> + <dd>The value of the <code>length</code> property is 0.</dd> + <dt>{{jsxref("Set.@@species", "get Set[@@species]")}}</dt> + <dd>The constructor function that is used to create derived objects.</dd> + <dt>{{jsxref("Set.prototype")}}</dt> + <dd>Represents the prototype for the <code>Set</code> constructor. Allows the addition of properties to all <code>Set</code> objects.</dd> +</dl> + +<h2 id="Set_物件實體"><code>Set</code> 物件實體</h2> + +<p>All <code>Set</code> instances inherit from {{jsxref("Set.prototype")}}.</p> + +<h3 id="屬性">屬性</h3> + +<p>{{page('en-US/Web/JavaScript/Reference/Global_Objects/Set/prototype','Properties')}}</p> + +<h3 id="方法">方法</h3> + +<p>{{page('en-US/Web/JavaScript/Reference/Global_Objects/Set/prototype','Methods')}}</p> + +<h2 id="範例">範例</h2> + +<h3 id="使用_Set_物件">使用 <code>Set</code> 物件</h3> + +<pre class="brush: js notranslate">var mySet = new Set(); + +mySet.add(1); // Set [ 1 ] +mySet.add(5); // Set [ 1, 5 ] +mySet.add(5); // Set [ 1, 5 ] +mySet.add('some text'); // Set [ 1, 5, 'some text' ] +var o = {a: 1, b: 2}; +mySet.add(o); + +mySet.add({a: 1, b: 2}); // o is referencing a different object so this is okay + +mySet.has(1); // true +mySet.has(3); // false, 3 has not been added to the set +mySet.has(5); // true +mySet.has(Math.sqrt(25)); // true +mySet.has('Some Text'.toLowerCase()); // true +mySet.has(o); // true + +mySet.size; // 5 + +mySet.delete(5); // removes 5 from the set +mySet.has(5); // false, 5 has been removed + +mySet.size; // 4, we just removed one value +console.log(mySet);// Set [ 1, "some text", Object {a: 1, b: 2}, Object {a: 1, b: 2} ]</pre> + +<h3 id="迭代_Sets">迭代 Sets</h3> + +<pre class="brush: js notranslate">// iterate over items in set +// logs the items in the order: 1, "some text", {"a": 1, "b": 2}, {"a": 1, "b": 2} +for (let item of mySet) console.log(item); + +// logs the items in the order: 1, "some text", {"a": 1, "b": 2}, {"a": 1, "b": 2} +for (let item of mySet.keys()) console.log(item); + +// logs the items in the order: 1, "some text", {"a": 1, "b": 2}, {"a": 1, "b": 2} +for (let item of mySet.values()) console.log(item); + +// logs the items in the order: 1, "some text", {"a": 1, "b": 2}, {"a": 1, "b": 2} +//(key and value are the same here) +for (let [key, value] of mySet.entries()) console.log(key); + +// convert Set object to an Array object, with <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from">Array.from</a> +var myArr = Array.from(mySet); // [1, "some text", {"a": 1, "b": 2}, {"a": 1, "b": 2}] + +// the following will also work if run in an HTML document +mySet.add(document.body); +mySet.has(document.querySelector('body')); // true + +// converting between Set and Array +mySet2 = new Set([1, 2, 3, 4]); +mySet2.size; // 4 +[...mySet2]; // [1, 2, 3, 4] + +// intersect can be simulated via +var intersection = new Set([...set1].filter(x => set2.has(x))); + +// difference can be simulated via +var difference = new Set([...set1].filter(x => !set2.has(x))); + +// Iterate set entries with forEach +mySet.forEach(function(value) { + console.log(value); +}); + +// 1 +// 2 +// 3 +// 4</pre> + +<h3 id="實作基本的_set_操作">實作基本的 set 操作</h3> + +<pre class="brush: js notranslate">Set.prototype.isSuperset = function(subset) { + for (var elem of subset) { + if (!this.has(elem)) { + return false; + } + } + return true; +} + +Set.prototype.union = function(setB) { + var union = new Set(this); + for (var elem of setB) { + union.add(elem); + } + return union; +} + +Set.prototype.intersection = function(setB) { + var intersection = new Set(); + for (var elem of setB) { + if (this.has(elem)) { + intersection.add(elem); + } + } + return intersection; +} + +Set.prototype.difference = function(setB) { + var difference = new Set(this); + for (var elem of setB) { + difference.delete(elem); + } + return difference; +} + +//Examples +var setA = new Set([1, 2, 3, 4]), + setB = new Set([2, 3]), + setC = new Set([3, 4, 5, 6]); + +setA.isSuperset(setB); // => true +setA.union(setC); // => Set [1, 2, 3, 4, 5, 6] +setA.intersection(setC); // => Set [3, 4] +setA.difference(setC); // => Set [1, 2] + +</pre> + +<h3 id="與_Array_物件關聯">與 <code>Array</code> 物件關聯</h3> + +<pre class="brush: js notranslate">var myArray = ['value1', 'value2', 'value3']; + +// Use the regular Set constructor to transform an Array into a Set +var mySet = new Set(myArray); + +mySet.has('value1'); // returns true + +// Use the spread operator to transform a set into an Array. +console.log([...mySet]); // Will show you exactly the same Array as myArray</pre> + +<h3 id="與_Strings_關聯">與 <code>Strings</code> 關聯</h3> + +<pre class="brush: js notranslate">var text = 'India'; + +var mySet = new Set(text); // Set ['I', 'n', 'd', 'i', 'a'] +mySet.size; // 5 +</pre> + +<h2 id="規範">規範</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + <tr> + <td>{{SpecName('ES2015', '#sec-set-objects', 'Set')}}</td> + <td>{{Spec2('ES2015')}}</td> + <td>Initial definition.</td> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-set-objects', 'Set')}}</td> + <td>{{Spec2('ESDraft')}}</td> + <td></td> + </tr> + </tbody> +</table> + +<h2 id="瀏覽器相容性">瀏覽器相容性</h2> + + + +<p>{{Compat("javascript.builtins.Set")}}</p> + +<h2 id="參見">參見</h2> + +<ul> + <li>{{jsxref("Map")}}</li> + <li>{{jsxref("WeakMap")}}</li> + <li>{{jsxref("WeakSet")}}</li> +</ul> diff --git a/files/zh-tw/web/javascript/reference/global_objects/set/values/index.html b/files/zh-tw/web/javascript/reference/global_objects/set/values/index.html new file mode 100644 index 0000000000..bb136ba806 --- /dev/null +++ b/files/zh-tw/web/javascript/reference/global_objects/set/values/index.html @@ -0,0 +1,79 @@ +--- +title: Set.prototype.values() +slug: Web/JavaScript/Reference/Global_Objects/Set/values +tags: + - ECMAScript 2015 + - JavaScript + - 原型 + - 方法 + - 迭代器 + - 集合 +translation_of: Web/JavaScript/Reference/Global_Objects/Set/values +--- +<div>{{JSRef}}</div> + +<p><code><strong>values()</strong></code> 方法回傳一個 <code>Iterator</code> 物件,包含著 <code>Set</code> 物件中所有元素,由插入順序排序。</p> + +<p><strong><code>keys()</code></strong> 是這個方法的替身 (為了與 {{jsxref("Map")}} 物件保持相似性);他運行的完全一模一樣,回傳 <code>Set</code> 中元素的 <strong>values</strong> 。</p> + +<div>{{EmbedInteractiveExample("pages/js/set-prototype-values.html")}}</div> + + + +<h2 id="語法">語法</h2> + +<pre class="syntaxbox"><code><em>mySet</em>.values(); +</code></pre> + +<h3 id="回傳值">回傳值</h3> + +<p>一個 <code>Iterator</code> 物件,包含著 <code>Set</code> 物件中所有元素,由插入順序排序。</p> + +<h2 id="範例">範例</h2> + +<h3 id="使用_values()">使用 <code>values()</code></h3> + +<pre class="brush:js">var mySet = new Set(); +mySet.add('foo'); +mySet.add('bar'); +mySet.add('baz'); + +var setIter = mySet.values(); + +console.log(setIter.next().value); // "foo" +console.log(setIter.next().value); // "bar" +console.log(setIter.next().value); // "baz"</pre> + +<h2 id="規範">規範</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">規範</th> + <th scope="col">狀態</th> + <th scope="col">註</th> + </tr> + <tr> + <td>{{SpecName('ES2015', '#sec-set.prototype.values', 'Set.prototype.values')}}</td> + <td>{{Spec2('ES2015')}}</td> + <td>Initial definition.</td> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-set.prototype.values', 'Set.prototype.values')}}</td> + <td>{{Spec2('ESDraft')}}</td> + <td> </td> + </tr> + </tbody> +</table> + +<h2 id="瀏覽器相容性">瀏覽器相容性</h2> + + + +<p>{{Compat("javascript.builtins.Set.values")}}</p> + +<h2 id="另見">另見</h2> + +<ul> + <li>{{jsxref("Set.prototype.entries()")}}</li> +</ul> |