diff options
| author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
|---|---|---|
| committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
| commit | 33058f2b292b3a581333bdfb21b8f671898c5060 (patch) | |
| tree | 51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/javascript/reference/global_objects/set | |
| parent | 8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff) | |
| download | translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2 translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip | |
initial commit
Diffstat (limited to 'files/zh-cn/web/javascript/reference/global_objects/set')
12 files changed, 1335 insertions, 0 deletions
diff --git a/files/zh-cn/web/javascript/reference/global_objects/set/@@iterator/index.html b/files/zh-cn/web/javascript/reference/global_objects/set/@@iterator/index.html new file mode 100644 index 0000000000..fa3f7c61e5 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/set/@@iterator/index.html @@ -0,0 +1,81 @@ +--- +title: 'Set.prototype[@@iterator]()' +slug: Web/JavaScript/Reference/Global_Objects/Set/@@iterator +tags: + - ECMAScript2015 + - Iterator + - JavaScript + - Method + - set +translation_of: Web/JavaScript/Reference/Global_Objects/Set/@@iterator +--- +<div>{{JSRef}}</div> + +<p><code><strong>@@iterator</strong></code> 属性的初始值和 {{jsxref("Set.prototype.values()", "values")}} 属性的初始值是同一个函数。</p> + +<div>{{EmbedInteractiveExample("pages/js/set-prototype-@@iterator.html")}}</div> + + + +<h2 id="Syntax">Syntax</h2> + +<pre class="syntaxbox notranslate"><code><em>mySet</em>[Symbol.iterator]</code></pre> + +<h3 id="Return_value">Return value</h3> + +<p>返回 <code>Set</code> <strong>iterator</strong> 函数,默认值是 {{jsxref("Set.prototype.values()", "values()")}} 函数。</p> + +<h2 id="Examples">Examples</h2> + +<h3 id="Using_iterator">Using <code>[@@iterator]()</code></h3> + +<pre class="brush:js notranslate">const mySet = new Set(); +mySet.add('0'); +mySet.add(1); +mySet.add({}); + +const setIter = mySet[Symbol.iterator](); + +console.log(setIter.next().value); // "0" +console.log(setIter.next().value); // 1 +console.log(setIter.next().value); // Object +</pre> + +<h3 id="Using_iterator_with_for..of">Using <code>[@@iterator]()</code> with <code>for..of</code></h3> + +<pre class="brush:js notranslate">const mySet = new Set(); +mySet.add('0'); +mySet.add(1); +mySet.add({}); + +for (const v of mySet) { + console.log(v); +} +</pre> + +<h2 id="Specifications">Specifications</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Specification</th> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-set.prototype-@@iterator', 'Set.prototype[@@iterator]')}}</td> + </tr> + </tbody> +</table> + +<h2 id="Browser_compatibility">Browser compatibility</h2> + + + +<p>{{Compat("javascript.builtins.Set.@@iterator")}}</p> + +<h2 id="See_also">See also</h2> + +<ul> + <li>{{jsxref("Set.prototype.entries()")}}</li> + <li>{{jsxref("Set.prototype.keys()")}}</li> + <li>{{jsxref("Set.prototype.values()")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/global_objects/set/@@species/index.html b/files/zh-cn/web/javascript/reference/global_objects/set/@@species/index.html new file mode 100644 index 0000000000..c950ccbf5a --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/set/@@species/index.html @@ -0,0 +1,61 @@ +--- +title: 'get Set[@@species]' +slug: Web/JavaScript/Reference/Global_Objects/Set/@@species +tags: + - ECMAScript2015 + - JavaScript + - set +translation_of: Web/JavaScript/Reference/Global_Objects/Set/@@species +--- +<div>{{JSRef}}</div> + +<p><code><strong>Set[@@species]</strong></code> 访问器属性返回<code>Set</code>的构造函数.</p> + +<h2 id="描述">描述</h2> + +<p>species 访问属性返回 <code>Set</code> 对象的默认构造函数. 子构造函数或许会重载这个属性以至改变构造函数的赋值.</p> + +<h2 id="示例">示例</h2> + +<h3 id="普通对象中的_Species">普通对象中的 Species</h3> + +<p>species 属性返回默认的构造函数, 它是<code>Set</code> 对象的构造函数:</p> + +<pre class="brush: js notranslate">Set[Symbol.species]; // function Set()</pre> + +<h3 id="派生对象中的_Species">派生对象中的 Species</h3> + +<p>在一个派生集合对象中 (比如你自定义的<code>MySet</code>集合), <code>MySet</code> 的species 属性 是 <code>MySet</code> 构造函数. 又或者, 你想要重写它, 让它能在你派生的类方法中能返回父级<code>Set</code> 对象:</p> + +<pre class="brush: js notranslate">class MySet extends Set { + // Overwrite MySet species to the parent Set constructor + static get [Symbol.species]() { return Set; } +}</pre> + +<h2 id="规范">规范</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Specification</th> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-get-set-@@species', 'get Set [ @@species ]')}}</td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<div> + + +<p>{{Compat("javascript.builtins.Set.@@species")}}</p> +</div> + +<h2 id="另见">另见</h2> + +<ul> + <li>{{jsxref("Set")}}</li> + <li>{{jsxref("Symbol.species")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/global_objects/set/add/index.html b/files/zh-cn/web/javascript/reference/global_objects/set/add/index.html new file mode 100644 index 0000000000..255379d70c --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/set/add/index.html @@ -0,0 +1,77 @@ +--- +title: Set.prototype.add() +slug: Web/JavaScript/Reference/Global_Objects/Set/add +tags: + - ECMAScript6 + - JavaScript + - Prototype + - set + - 原型 + - 方法 +translation_of: Web/JavaScript/Reference/Global_Objects/Set/add +--- +<div>{{JSRef}} </div> + +<p><code><strong>add()</strong></code> 方法用来向一个 <code>Set</code> 对象的末尾添加一个指定的值。</p> + +<div>{{EmbedInteractiveExample("pages/js/set-prototype-add.html")}}</div> + + + +<h2 id="Syntax" name="Syntax">语法</h2> + +<pre class="syntaxbox notranslate"><code><em>mySet</em>.add(value);</code></pre> + +<h3 id="参数">参数</h3> + +<dl> + <dt>value</dt> + <dd>必需。需要添加到 <code>Set </code>对象的元素的值。</dd> +</dl> + +<h3 id="返回值">返回值</h3> + +<p><code>Set</code> 对象本身</p> + +<p>注意:不能添加重复的值</p> + +<h2 id="Examples" name="Examples">示例</h2> + +<pre class="brush: js notranslate">var mySet = new Set(); + +mySet.add(1); +mySet.add(5).add("some text"); // 可以链式调用 + +console.log(mySet); +// Set [1, 5, "some text"] + +mySet.add(5).add(1); +console.log(mySet); +// Set [1, 5, "some text"] // 重复的值没有被添加进去</pre> + +<h2 id="规范">规范</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">规范</th> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-set.prototype.add', 'Set.prototype.add')}}</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-cn/web/javascript/reference/global_objects/set/clear/index.html b/files/zh-cn/web/javascript/reference/global_objects/set/clear/index.html new file mode 100644 index 0000000000..6103b63a67 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/set/clear/index.html @@ -0,0 +1,120 @@ +--- +title: Set.prototype.clear() +slug: Web/JavaScript/Reference/Global_Objects/Set/clear +tags: + - ECMAScript6 + - JavaScript + - Prototype + - set + - 原型 + - 方法 +translation_of: Web/JavaScript/Reference/Global_Objects/Set/clear +--- +<div>{{JSRef}}</div> + +<p><code><strong>clear()</strong></code> 方法用来清空一个 <code>Set</code> 对象中的所有元素。</p> + +<h2 id="Syntax" name="Syntax">语法</h2> + +<pre class="syntaxbox"><code><em>mySet</em>.clear();</code> +</pre> + +<h3 id="返回值">返回值</h3> + +<p>{{jsxref("undefined")}}.</p> + +<h2 id="Examples" name="Examples">示例</h2> + +<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">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + <tr> + <td>{{SpecName('ES6', '#sec-set.prototype.clear', 'Set.prototype.clear')}}</td> + <td>{{Spec2('ES6')}}</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>{{ CompatibilityTable() }}</p> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari</th> + </tr> + <tr> + <td>Basic support</td> + <td>38</td> + <td>{{CompatGeckoDesktop("19.0")}}</td> + <td>11</td> + <td>25</td> + <td>7.1</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Android</th> + <th>Chrome for Android</th> + <th>Firefox Mobile (Gecko)</th> + <th>IE Mobile</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{ CompatNo() }}</td> + <td>25</td> + <td>{{CompatGeckoMobile("19.0")}}</td> + <td>{{ CompatNo() }}</td> + <td>{{ CompatNo() }}</td> + <td>8</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="相关链接"><strong style="font-size: 2.14285714285714rem; font-weight: 700; letter-spacing: -1px; line-height: 30px;">相关链接</strong></h2> + +<ul> + <li>{{jsxref("Set")}}</li> + <li>{{jsxref("Set.prototype.delete()")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/global_objects/set/delete/index.html b/files/zh-cn/web/javascript/reference/global_objects/set/delete/index.html new file mode 100644 index 0000000000..5602ae7942 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/set/delete/index.html @@ -0,0 +1,120 @@ +--- +title: Set.prototype.delete() +slug: Web/JavaScript/Reference/Global_Objects/Set/delete +tags: + - ECMAScript6 + - JavaScript + - set + - 方法 +translation_of: Web/JavaScript/Reference/Global_Objects/Set/delete +--- +<div>{{JSRef}}</div> + +<p><code><strong>delete()</strong></code> 方法可以从一个 <code>Set</code> 对象中删除指定的元素。</p> + +<h2 id="Syntax" name="Syntax">语法</h2> + +<pre class="syntaxbox"><code><em>mySet</em>.delete(value);</code></pre> + +<h3 id="参数">参数</h3> + +<dl> + <dt>value</dt> + <dd>将要删除的元素</dd> +</dl> + +<h3 id="返回值">返回值</h3> + +<p>成功删除返回 <code>true</code>,否则返回 <code>false。</code></p> + +<h2 id="Examples" name="Examples">示例</h2> + +<pre class="brush: js">var mySet = new Set(); +mySet.add("foo"); + +mySet.delete("bar"); // 返回 false,不包含 "bar" 这个元素 +mySet.delete("foo"); // 返回 true,删除成功 + +mySet.has("foo"); // 返回 false,"foo" 已经成功删除 +</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('ES6', '#sec-set.prototype.delete', 'Set.prototype.delete')}}</td> + <td>{{Spec2('ES6')}}</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>{{ CompatibilityTable() }}</p> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari</th> + </tr> + <tr> + <td>Basic support</td> + <td>38</td> + <td>{{CompatGeckoDesktop("13.0")}}</td> + <td>11</td> + <td>25</td> + <td>7.1</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Android</th> + <th>Chrome for Android</th> + <th>Firefox Mobile (Gecko)</th> + <th>IE Mobile</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{ CompatNo() }}</td> + <td>38</td> + <td>{{CompatGeckoMobile("13.0")}}</td> + <td>{{ CompatNo() }}</td> + <td>{{ CompatNo() }}</td> + <td>8</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="See_also" name="See_also">相关链接</h2> + +<ul> + <li>{{jsxref("Set")}}</li> + <li>{{jsxref("Set.prototype.clear()")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/global_objects/set/entries/index.html b/files/zh-cn/web/javascript/reference/global_objects/set/entries/index.html new file mode 100644 index 0000000000..f73a1e1a26 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/set/entries/index.html @@ -0,0 +1,113 @@ +--- +title: Set.prototype.entries() +slug: Web/JavaScript/Reference/Global_Objects/Set/entries +translation_of: Web/JavaScript/Reference/Global_Objects/Set/entries +--- +<div>{{JSRef}}</div> + +<p>entries() 方法返回一个新的迭代器对象 ,这个对象的元素是类似 [value, value] 形式的数组,value 是集合对象中的每个元素,迭代器对象元素的顺序即集合对象中元素插入的顺序。由于集合对象不像 Map 对象那样拥有 key,然而,为了与 Map 对象的 API 形式保持一致,故使得每一个 entry 的 key 和 value 都拥有相同的值,因而最终返回一个 [value, value] 形式的数组。</p> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox"><code><em>mySet</em>.entries()</code></pre> + +<h3 id="返回值">返回值</h3> + +<p>一个新的包含 [value, value] 形式的数组迭代器对象,value 是给定集合中的每个元素,迭代器 对象元素的顺序即集合对象中元素插入的顺序。</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('ES6', '#sec-set.prototype.entries', 'Set.prototype.entries')}}</td> + <td>{{Spec2('ES6')}}</td> + <td>初始定义。</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>{{CompatibilityTable}}</p> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari</th> + </tr> + <tr> + <td>Basic support</td> + <td>38</td> + <td>{{ CompatGeckoDesktop("24") }}</td> + <td>{{CompatNo}}</td> + <td>25</td> + <td>7.1</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Android</th> + <th>Chrome for Android</th> + <th>Firefox Mobile (Gecko)</th> + <th>IE Mobile</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{CompatNo}}</td> + <td>38</td> + <td>{{ CompatGeckoMobile("24") }}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>8</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="相关链接">相关链接</h2> + +<ul> + <li>{{jsxref("Set.prototype.keys()")}}</li> + <li>{{jsxref("Set.prototype.values()")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/global_objects/set/foreach/index.html b/files/zh-cn/web/javascript/reference/global_objects/set/foreach/index.html new file mode 100644 index 0000000000..e89be3af36 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/set/foreach/index.html @@ -0,0 +1,145 @@ +--- +title: Set.prototype.forEach() +slug: Web/JavaScript/Reference/Global_Objects/Set/forEach +translation_of: Web/JavaScript/Reference/Global_Objects/Set/forEach +--- +<div>{{JSRef}}</div> + +<p><code>forEach</code> 方法会根据集合中元素的插入顺序,依次执行提供的回调函数。</p> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox"><code><em>mySet</em>.forEach(<em>callback</em>[, <em>thisArg</em>])</code></pre> + +<h3 id="参数">参数</h3> + +<dl> + <dt><code>callback</code></dt> + <dd>为集合中每个元素执行的回调函数,该函数接收三个参数:</dd> + <dd> + <dl> + <dt><strong><font face="consolas, Liberation Mono, courier, monospace"><span style="background-color: rgba(220, 220, 220, 0.5);">currentValue</span>, </font></strong><strong><font face="consolas, Liberation Mono, courier, monospace"><span style="background-color: rgba(220, 220, 220, 0.5);">currentKey</span>{{optional_inline}}</font></strong></dt> + <dd><strong>currentValue</strong> 是正在被操作的元素。并且由于集合没有索引,所以 <strong>currentKey</strong> 也表示这个正在被操作的元素。</dd> + <dt><strong><font face="consolas, Liberation Mono, courier, monospace"><span style="background-color: rgba(220, 220, 220, 0.5);">set</span>{{optional_inline}}</font></strong></dt> + <dd>调用当前 <code>forEach</code> 方法的集合对象</dd> + </dl> + </dd> + <dt><code>thisArg</code><strong><font face="consolas, Liberation Mono, courier, monospace">{{optional_inline}}</font></strong></dt> + <dd>回调函数执行过程中的 <code>this</code> 值。</dd> +</dl> + +<h3 id="返回值">返回值</h3> + +<p>{{jsxref("undefined")}}.</p> + +<h2 id="描述">描述</h2> + +<p><code>forEach</code> 方法会依次为集合中的元素执行回调函数,就算元素的值是 <code>undefined </code>。</p> + +<p><strong>回调函数</strong>有三个参数:</p> + +<ul> + <li>元素的值</li> + <li>元素的索引</li> + <li>正在遍历的集合对象</li> +</ul> + +<p>但是由于集合对象中没有索引(keys),所以前两个参数都是{{domxref("Set")}}中元素的值(<strong>values</strong>),之所以这样设计回调函数是为了和{{jsxref("Map.foreach", "Map")}} 以及{{jsxref("Array.forEach","Array")}}的 <code>forEach</code> 函数用法保持一致。</p> + +<p>如果提供了一个 <code>thisArg</code> 参数给 <code>forEach</code> 函数,则参数将会作为回调函数中的 <code>this</code>值。否则 <code>this</code> 值为 <code>undefined</code>。回调函数中 <code>this</code> 的绑定是根据<a href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/this">函数被调用时通用的 <code>this</code> 绑定规则来决定的</a>。</p> + +<p><code>forEach</code> 函数为集合对象中每个元素都执行一次回调;它不会返回任何值。</p> + +<h2 id="例子">例子</h2> + +<h3 id="输出集合对象的内容">输出集合对象的内容</h3> + +<p><span class="outputBox-2liU7_0">以下代码依次打印集合对象的元素:</span></p> + +<pre class="brush:js">function logSetElements(value1, value2, set) { + console.log("s[" + value1 + "] = " + value2); +} + +new Set(["foo", "bar", undefined]).forEach(logSetElements); + +// logs: +// "s[foo] = foo" +// "s[bar] = bar" +// "s[undefined] = undefined" +</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('ES6', '#sec-set.prototype.foreach', 'Set.prototype.forEach')}}</td> + <td>{{Spec2('ES6')}}</td> + <td>Initial definition.</td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<p>{{CompatibilityTable}}</p> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{ CompatChrome(38) }}</td> + <td>{{CompatGeckoDesktop("25.0")}}</td> + <td>{{ CompatIE("11") }}</td> + <td>25</td> + <td>7.1</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Android</th> + <th>Chrome for Android</th> + <th>Firefox Mobile (Gecko)</th> + <th>IE Mobile</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{CompatNo}}</td> + <td>38</td> + <td>{{CompatGeckoMobile("25.0")}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>8</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="参见">参见</h2> + +<ul> + <li>{{jsxref("Array.prototype.forEach()")}}</li> + <li>{{jsxref("Map.prototype.forEach()")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/global_objects/set/has/index.html b/files/zh-cn/web/javascript/reference/global_objects/set/has/index.html new file mode 100644 index 0000000000..e9edc5d978 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/set/has/index.html @@ -0,0 +1,127 @@ +--- +title: Set.prototype.has() +slug: Web/JavaScript/Reference/Global_Objects/Set/has +tags: + - ECMAScript 2015 + - JavaScript + - set +translation_of: Web/JavaScript/Reference/Global_Objects/Set/has +--- +<div>{{JSRef("Global_Objects", "Set")}}</div> + +<h2 id="Summary" name="Summary">概述</h2> + +<p><strong>has() </strong>方法返回一个布尔值来指示对应的值value是否存在Set对象中。</p> + +<h2 id="Syntax" name="Syntax">语法</h2> + +<pre class="syntaxbox"><code><em>mySet</em>.has(value);</code></pre> + +<h3 id="参数">参数</h3> + +<dl> + <dt>value</dt> + <dd>必需。用以测试该值是否存在于 Set 对象中。</dd> +</dl> + +<h3 id="返回值">返回值</h3> + +<dl> + <dt>Boolean</dt> + <dd>如果指定的值(value)存在于Set对象当中,返回<code>true</code>;否则返回 <code>false</code>。</dd> +</dl> + +<h2 id="Examples" name="Examples">示例</h2> + +<h3 id="Example:_Testing_size_of_all_array_elements" name="Example:_Testing_size_of_all_array_elements">使用 <code>has</code> 方法</h3> + +<pre class="brush: js line-numbers language-js"><code class="language-js"><span class="keyword token">var</span> mySet <span class="operator token">=</span> <span class="keyword token">new</span> <span class="class-name token">Set</span><span class="punctuation token">(</span><span class="punctuation token">)</span><span class="punctuation token">;</span> +mySet<span class="punctuation token">.</span><span class="function token">add</span><span class="punctuation token">(</span><span class="string token">'foo'</span><span class="punctuation token">)</span><span class="punctuation token">;</span> + +mySet<span class="punctuation token">.</span><span class="function token">has</span><span class="punctuation token">(</span><span class="string token">'foo'</span><span class="punctuation token">)</span><span class="punctuation token">;</span> <span class="comment token">// 返回 true</span> +mySet<span class="punctuation token">.</span><span class="function token">has</span><span class="punctuation token">(</span><span class="string token">'bar'</span><span class="punctuation token">)</span><span class="punctuation token">;</span> <span class="comment token">// 返回 false</span> + +<span class="keyword token">var</span> set1 <span class="operator token">=</span> <span class="keyword token">new</span> <span class="class-name token">Set</span><span class="punctuation token">(</span><span class="punctuation token">)</span><span class="punctuation token">;</span> +<span class="keyword token">var</span> obj1 <span class="operator token">=</span> <span class="punctuation token">{</span><span class="string token">'key1'</span><span class="punctuation token">:</span> <span class="number token">1</span><span class="punctuation token">}</span><span class="punctuation token">;</span> +set1<span class="punctuation token">.</span><span class="function token">add</span><span class="punctuation token">(</span>obj1<span class="punctuation token">)</span><span class="punctuation token">;</span> + +set1<span class="punctuation token">.</span><span class="function token">has</span><span class="punctuation token">(</span>obj1<span class="punctuation token">)</span><span class="punctuation token">;</span> <span class="comment token">// 返回 true</span> +set1<span class="punctuation token">.</span><span class="function token">has</span><span class="punctuation token">(</span><span class="punctuation token">{</span><span class="string token">'key1'</span><span class="punctuation token">:</span> <span class="number token">1</span><span class="punctuation token">}</span><span class="punctuation token">)</span><span class="punctuation token">;</span> <span class="comment token">// 会返回 false,因为其是另一个对象的引用</span> +set1<span class="punctuation token">.</span><span class="function token">add</span><span class="punctuation token">(</span><span class="punctuation token">{</span><span class="string token">'key1'</span><span class="punctuation token">:</span> <span class="number token">1</span><span class="punctuation token">}</span><span class="punctuation token">)</span><span class="punctuation token">;</span> <span class="comment token">// 现在 set1 中有2条(不同引用的)对象了</span></code></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('ES6', '#sec-set.prototype.has', 'Set.prototype.has')}}</td> + <td>{{Spec2('ES6')}}</td> + <td>Initial definition.</td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<p>{{ CompatibilityTable() }}</p> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari</th> + </tr> + <tr> + <td>Basic support</td> + <td>38</td> + <td>{{CompatGeckoDesktop("13.0")}}</td> + <td>11</td> + <td>25</td> + <td>7.1</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Android</th> + <th>Chrome for Android</th> + <th>Firefox Mobile (Gecko)</th> + <th>IE Mobile</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{ CompatNo() }}</td> + <td>38</td> + <td>{{CompatGeckoMobile("13.0")}}</td> + <td>{{ CompatNo() }}</td> + <td>{{ CompatNo() }}</td> + <td>iOS 8</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="相关连接"><span style="font-size: 30px; letter-spacing: -1px; line-height: 30px;"><strong>相关连接</strong></span></h2> + +<ul> + <li>{{jsxref("Set")}}</li> + <li>{{jsxref("Set.prototype.add()")}}</li> + <li>{{jsxref("Set.prototype.delete()")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/global_objects/set/index.html b/files/zh-cn/web/javascript/reference/global_objects/set/index.html new file mode 100644 index 0000000000..6d4dca88c9 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/set/index.html @@ -0,0 +1,273 @@ +--- +title: Set +slug: Web/JavaScript/Reference/Global_Objects/Set +tags: + - Class + - ECMAScript6 + - JavaScript + - set + - 全局对象 +translation_of: Web/JavaScript/Reference/Global_Objects/Set +--- +<div>{{ JSRef }}</div> + +<p><strong><code>Set</code></strong> 对象允许你存储任何类型的唯一值,无论是{{ Glossary('Primitive', '原始值') }}或者是对象引用。</p> + +<h2 id="简述">简述</h2> + +<p><code>Set</code>对象是值的集合,你可以按照插入的顺序迭代它的元素。 Set中的元素只会<strong>出现一次</strong>,即 Set 中的元素是唯一的。</p> + +<h3 id="值的相等">值的相等</h3> + +<p>因为 Set 中的值总是唯一的,所以需要判断两个值是否相等。在ECMAScript规范的早期版本中,这不是基于和===操作符中使用的算法相同的算法。具体来说,对于 Set s, +0 (+0 严格相等于-0)和-0是不同的值。然而,在 ECMAScript 2015规范中这点已被更改。有关详细信息,请参阅<a href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Set#浏览器兼容性">浏览器兼容性</a> 表中的“<em>Key equality for -0 and 0</em>”。</p> + +<p>另外,<code>NaN</code>和<code>undefined</code>都可以被存储在Set 中, <code>NaN</code>之间被视为相同的值(NaN被认为是相同的,尽管 NaN !== NaN)。</p> + +<h2 id="Constructor">Constructor</h2> + +<dl> + <dt><a href="https://wiki.developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set/Set"><code>Set()</code></a></dt> + <dd>创建一个新的<code>Set</code>对象。</dd> +</dl> + +<h2 id="静态属性">静态属性</h2> + +<dl> + <dt>{{jsxref("Set.@@species", "get Set[@@species]")}}</dt> + <dd>构造函数用来创建派生对象.</dd> +</dl> + +<h2 id="Properties" name="Properties">实例属性</h2> + +<dl> + <dt>{{jsxref("Set.prototype.size")}}</dt> + <dd>返回 Set 对象中的值的个数</dd> +</dl> + +<h2 id="实例方法">实例方法</h2> + +<dl> + <dt>{{jsxref("Set.add", "Set.prototype.add(<em>value</em>)")}}</dt> + <dd>在<code>Set</code>对象尾部添加一个元素。返回该<code>Set</code>对象。</dd> + <dt>{{jsxref("Set.prototype.clear()")}}</dt> + <dd>移除<code>Set</code>对象内的所有元素。</dd> + <dt>{{jsxref("Set.delete", "Set.prototype.delete(<em>value</em>)")}}</dt> + <dd>移除<code>Set</code>中与这个值相等的元素,返回<code>Set.prototype.has(value)</code>在这个操作前会返回的值(即如果该元素存在,返回<code>true</code>,否则返回<code>false</code>)。<code>Set.prototype.has(value)</code>在此后会返回<code>false</code>。</dd> + <dt>{{jsxref("Set.prototype.entries()")}}</dt> + <dd>返回一个新的迭代器对象,该对象包含<code>Set</code>对象中的按插入顺序排列的所有元素的值的<code>[value, value]</code>数组。为了使这个方法和<code>Map</code>对象保持相似, 每个值的键和值相等。</dd> + <dt>{{jsxref("Set.forEach", "Set.prototype.forEach(<em>callbackFn</em>[, <em>thisArg</em>])")}}</dt> + <dd>按照插入顺序,为Set对象中的每一个值调用一次callBackFn。如果提供了<code>thisArg</code>参数,回调中的<code>this</code>会是这个参数。</dd> + <dt>{{jsxref("Set.has", "Set.prototype.has(<em>value</em>)")}}</dt> + <dd>返回一个布尔值,表示该值在<code>Set</code>中存在与否。</dd> + <dt>{{jsxref("Set.prototype.keys()")}}</dt> + <dd>与<strong><code>values()</code></strong>方法相同,返回一个新的迭代器对象,该对象包含<code>Set</code>对象中的按插入顺序排列的所有元素的值。</dd> + <dt>{{jsxref("Set.prototype.values()")}}</dt> + <dd>返回一个新的迭代器对象,该对象包含<code>Set</code>对象中的按插入顺序排列的所有元素的值。</dd> + <dt>{{jsxref("Set.prototype.@@iterator()", "Set.prototype[@@iterator]()")}}</dt> + <dd>返回一个新的迭代器对象,该对象包含<code>Set</code>对象中的按插入顺序排列的所有元素的值。</dd> +</dl> + +<h2 id="示例">示例</h2> + +<h3 id="使用Set对象">使用<code>Set</code>对象</h3> + +<pre class="brush: js notranslate">let 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" ] +let o = {a: 1, b: 2}; +mySet.add(o); + +mySet.add({a: 1, b: 2}); // o 指向的是不同的对象,所以没问题 + +mySet.has(1); // true +mySet.has(3); // false +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); // true, 从set中移除5 +mySet.has(5); // false, 5已经被移除 + +mySet.size; // 4, 刚刚移除一个值 + +console.log(mySet); +// logs Set(4) [ 1, "some text", {…}, {…} ] in Firefox +// logs Set(4) { 1, "some text", {…}, {…} } in Chrome</pre> + +<h3 id="迭代Set">迭代Set</h3> + +<pre class="brush: js notranslate">// 迭代整个set +// 按顺序输出:1, "some text", {"a": 1, "b": 2}, {"a": 1, "b": 2} +for (let item of mySet) console.log(item); + +// 按顺序输出:1, "some text", {"a": 1, "b": 2}, {"a": 1, "b": 2} +for (let item of mySet.keys()) console.log(item); + +// 按顺序输出:1, "some text", {"a": 1, "b": 2}, {"a": 1, "b": 2} +for (let item of mySet.values()) console.log(item); + +// 按顺序输出:1, "some text", {"a": 1, "b": 2}, {"a": 1, "b": 2} +//(键与值相等) +for (let [key, value] of mySet.entries()) console.log(key); + +// 使用 Array.from 转换Set为Array +var myArr = Array.from(mySet); // [1, "some text", {"a": 1, "b": 2}, {"a": 1, "b": 2}] + +// 如果在HTML文档中工作,也可以: +mySet.add(document.body); +mySet.has(document.querySelector("body")); // true + +// Set 和 Array互换 +mySet2 = new Set([1, 2, 3, 4]); +mySet2.size; // 4 +[...mySet2]; // [1,2,3,4] + +// 可以通过如下代码模拟求交集 +let intersection = new Set([...set1].filter(x => set2.has(x))); + +// 可以通过如下代码模拟求差集 +let difference = new Set([...set1].filter(x => !set2.has(x))); + +// 用forEach迭代 +mySet.forEach(function(value) { + console.log(value); +}); + +// 1 +// 2 +// 3 +// 4 +</pre> + +<h3 id="实现基本集合操作">实现基本集合操作</h3> + +<pre class="brush: js notranslate">function isSuperset(set, subset) { + for (let elem of subset) { + if (!set.has(elem)) { + return false; + } + } + return true; +} + +function union(setA, setB) { + let _union = new Set(setA); + for (let elem of setB) { + _union.add(elem); + } + return _union; +} + +function intersection(setA, setB) { + let _intersection = new Set(); + for (let elem of setB) { + if (setA.has(elem)) { + _intersection.add(elem); + } + } + return _intersection; +} + +<code>function symmetricDifference(setA, setB) { + let _difference = new Set(setA); + for (let elem of setB) { + if (_difference.has(elem)) { + _difference.delete(elem); + } else { + _difference.add(elem); + } + } + return _difference; +}</code> + +function difference(setA, setB) { + let _difference = new Set(setA); + for (let elem of setB) { + _difference.delete(elem); + } + return _difference; +} + +//Examples +let setA = new Set([1, 2, 3, 4]), + setB = new Set([2, 3]), + setC = new Set([3, 4, 5, 6]); + +isSuperset(setA, setB); // => true +union(setA, setC); // => Set [1, 2, 3, 4, 5, 6] +intersection(setA, setC); // => Set [3, 4] +symmetricDifference(setA, setC); // => Set [1, 2, 5, 6] +difference(setA, setC); // => Set [1, 2] +</pre> + +<h3 id="Array_相关"> <code>Array</code> 相关</h3> + +<pre class="brush: js notranslate">let myArray = ["value1", "value2", "value3"]; + +// 用Set构造器将Array转换为Set +let mySet = new Set(myArray); + +mySet.has("value1"); // returns true + +// 用...(展开操作符)操作符将Set转换为Array +console.log([...mySet]); // 与myArray完全一致 +</pre> + +<h3 id="数组去重">数组去重</h3> + +<pre class="brush: js notranslate">// Use to remove duplicate elements from the array +const numbers = [2,3,4,4,2,3,3,4,4,5,5,6,6,7,5,32,3,4,5] +console.log([...new Set(numbers)]) +// [2, 3, 4, 5, 6, 7, 32] +</pre> + +<h3 id="String_相关"><code>String</code> 相关</h3> + +<pre class="brush: js notranslate">let text = 'India'; + +let mySet = new Set(text); // Set {'I', 'n', 'd', 'i', 'a'} +mySet.size; // 5 + +// 大小写敏感 & duplicate ommision +new Set("Firefox") // Set(7) [ "F", "i", "r", "e", "f", "o", "x" ] +new Set("firefox") // Set(6) [ "f", "i", "r", "e", "o", "x" ] +</pre> + +<h2 id="规范">规范</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">规范</th> + </tr> + <tr> + <td>{{ SpecName('ESDraft', '#sec-set-objects', 'Set') }}</td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<div class="hidden"> +<p>The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> and send us a pull request.</p> +</div> + +<p>{{ Compat('javascript.builtins.Set') }}</p> + +<h2 id="参见">参见</h2> + +<ul> + <li>{{ jsxref('Map') }}</li> + <li>{{ jsxref('WeakMap') }}</li> + <li>{{ jsxref('WeakSet') }}</li> +</ul> + +<p> + <audio style="display: none;"></audio> +</p> diff --git a/files/zh-cn/web/javascript/reference/global_objects/set/set/index.html b/files/zh-cn/web/javascript/reference/global_objects/set/set/index.html new file mode 100644 index 0000000000..2ead74e30a --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/set/set/index.html @@ -0,0 +1,73 @@ +--- +title: Set() constructor +slug: Web/JavaScript/Reference/Global_Objects/Set/Set +tags: + - Constructor + - JavaScript + - Reference + - set +translation_of: Web/JavaScript/Reference/Global_Objects/Set/Set +--- +<div>{{JSRef}}</div> + +<p><span class="seoSummary"><strong><code>Set</code> 构造函数</strong>能让你创建 <code>Set</code> 对象,其可以存储任意类型的唯一值,无论是 <a href="/en-US/docs/Glossary/Primitive">primitive values</a> 或者对象引用。</span></p> + +<div>{{EmbedInteractiveExample("pages/js/set-prototype-constructor.html")}}</div> + + + +<h2 id="Syntax">Syntax</h2> + +<pre class="syntaxbox notranslate">new Set([<var>iterable</var>])</pre> + +<h3 id="Parameters">Parameters</h3> + +<dl> + <dt><em><code>iterable</code></em> {{optional_inline}}</dt> + <dd>如果传递一个<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of">可迭代对象</a>,它的所有元素将不重复地被添加到新的 <strong>Set</strong>中。</dd> + <dd>如果不指定此参数或其值为<code>null</code>,则新的 <strong>Set</strong>为空。</dd> +</dl> + +<h3 id="Return_value">Return value</h3> + +<p>A new <code>Set</code> object.</p> + +<h2 id="Examples">Examples</h2> + +<h3 id="Using_the_Set_object">Using the <code>Set</code> object</h3> + +<pre class="brush: js notranslate">let 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' ] +let o = {a: 1, b: 2} +mySet.add(o)</pre> + +<h2 id="Specifications">Specifications</h2> + +<table class="standard-table"> + <thead> + <tr> + <th scope="col">Specification</th> + </tr> + </thead> + <tbody> + <tr> + <td>{{SpecName('ESDraft', '#sec-set-constructor', 'Set constructor')}}</td> + </tr> + </tbody> +</table> + +<h2 id="Browser_compatibility">Browser compatibility</h2> + + + +<p>{{Compat("javascript.builtins.Set.Set")}}</p> + +<h2 id="See_also">See also</h2> + +<ul> + <li>{{jsxref("Set")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/global_objects/set/size/index.html b/files/zh-cn/web/javascript/reference/global_objects/set/size/index.html new file mode 100644 index 0000000000..a18e85fade --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/set/size/index.html @@ -0,0 +1,75 @@ +--- +title: Set.prototype.size +slug: Web/JavaScript/Reference/Global_Objects/Set/size +tags: + - ECMAScript 2015 + - JavaScript + - Property + - set +translation_of: Web/JavaScript/Reference/Global_Objects/Set/size +--- +<div>{{JSRef}}</div> + +<p><strong>Size</strong>属性将会返回{{jsxref("Set")}}对象中元素的个数。</p> + +<p>{{EmbedInteractiveExample("pages/js/set-prototype-size.html")}}</p> + +<div class="hidden"> +<p>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 <a href="https://github.com/mdn/interactive-examples">https://github.com/mdn/interactive-examples</a> and send us a pull request.</p> +</div> + +<h2 id="描述">描述</h2> + +<p><code>size</code>的值是一个整数,表示<code>Set</code>对象有多少条目。<code>size</code>的集合访问函数是<code>undefined</code>; 你不能改变这个属性。</p> + +<h2 id="例子">例子</h2> + +<h3 id="使用size">使用<code>size</code></h3> + +<pre class="brush:js">var mySet = new Set(); +mySet.add(1); +mySet.add(5); +mySet.add("some text") + +mySet.size; // 3 +</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> + <p>{{SpecName('ES6', '#sec-get-set.prototype.size', 'Set.prototype.size')}}</p> + </td> + <td>{{Spec2('ES6')}}</td> + <td>初始定义</td> + </tr> + <tr> + <td> + <p>{{SpecName('ESDraft', '#sec-get-set.prototype.size', 'Set.prototype.size')}}</p> + </td> + <td>{{Spec2('ESDraft')}}</td> + <td> </td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容">浏览器兼容</h2> + +<div class="hidden"> +<p>The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> and send us a pull request.</p> +</div> + +<p>{{Compat("javascript.builtins.Set.size")}}</p> + +<h2 id="参见">参见</h2> + +<ul> + <li>{{jsxref("Set")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/global_objects/set/values/index.html b/files/zh-cn/web/javascript/reference/global_objects/set/values/index.html new file mode 100644 index 0000000000..846bd7421d --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/set/values/index.html @@ -0,0 +1,70 @@ +--- +title: Set.prototype.values() +slug: Web/JavaScript/Reference/Global_Objects/Set/values +tags: + - ECMAScript 2015 + - Iterator + - JavaScript + - Method + - Prototype + - set +translation_of: Web/JavaScript/Reference/Global_Objects/Set/values +--- +<div>{{JSRef}}</div> + +<p><code><strong>values()</strong></code> 方法按照元素插入顺序返回一个具有 <code>Set</code> 对象每个元素值的全新 <code>Iterator</code> 对象。</p> + +<p><strong><code>keys()</code></strong> 方法是这个方法的别名(与 {{jsxref("Map")}} 对象相似);他们的行为一致,都是返回<code>Set</code> 对象中的元素值。</p> + +<div>{{EmbedInteractiveExample("pages/js/set-prototype-values.html")}}</div> + + + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox notranslate"><code><em>mySet</em>.values(); +</code></pre> + +<h3 id="返回值">返回值</h3> + +<p>按照元素插入顺序返回一个包含给定的 <code>Set</code> 对象中每个元素值的全新 <code><strong>Iterator</strong></code> 对象。</p> + +<h2 id="示例">示例</h2> + +<h3 id="使用_values">使用 <code>values()</code></h3> + +<pre class="brush:js notranslate">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> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-set.prototype.values', 'Set.prototype.values')}}</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> |
