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

AutoCorrect files/zh-cn/web/javascript/reference/global_objects/s* #5968

Merged
merged 13 commits into from
May 29, 2022
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,23 @@
---
<div>{{JSRef}}</div>

<p><code><strong>Set[@@species]</strong></code> 访问器属性返回<code>Set</code>的构造函数.</p>
<p><code><strong>Set[@@species]</strong></code> 访问器属性返回<code>Set</code>的构造函数</p>

<h2 id="描述">描述</h2>

<p>species 访问属性返回 <code>Set</code> 对象的默认构造函数. 子构造函数或许会重载这个属性以至改变构造函数的赋值.</p>
<p>species 访问属性返回 <code>Set</code> 对象的默认构造函数子构造函数或许会重载这个属性以至改变构造函数的赋值</p>

<h2 id="示例">示例</h2>

<h3 id="普通对象中的_Species">普通对象中的 Species</h3>

<p>species 属性返回默认的构造函数, 它是<code>Set</code> 对象的构造函数:</p>
<p>species 属性返回默认的构造函数它是<code>Set</code> 对象的构造函数</p>

<pre class="brush: js">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>
<p>在一个派生集合对象中 (比如你自定义的<code>MySet</code>集合),  <code>MySet</code> 的 species 属性 是 <code>MySet</code> 构造函数又或者你想要重写它让它能在你派生的类方法中能返回父级<code>Set</code> 对象</p>

<pre class="brush: js">class MySet extends Set {
// Overwrite MySet species to the parent Set constructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ <h2 id="描述">描述</h2>

<p><code>forEach</code> 方法会依次为集合中的元素执行回调函数,就算元素的值是 <code>undefined </code>。</p>

<p><strong>回调函数</strong>有三个参数:</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>但是由于集合对象中没有索引 (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>

Expand All @@ -53,7 +53,7 @@ <h2 id="例子">例子</h2>

<h3 id="输出集合对象的内容">输出集合对象的内容</h3>

<p>以下代码依次打印集合对象的元素:</p>
<p>以下代码依次打印集合对象的元素</p>

<pre class="brush:js">function logSetElements(value1, value2, set) {
console.log("s[" + value1 + "] = " + value2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

<h2 id="Summary">概述</h2>

<p><strong>has() </strong>方法返回一个布尔值来指示对应的值value是否存在Set对象中。</p>
<p><strong>has() </strong>方法返回一个布尔值来指示对应的值 value 是否存在 Set 对象中。</p>

<h2 id="Syntax">语法</h2>

Expand All @@ -28,7 +28,7 @@ <h3 id="返回值">返回值</h3>

<dl>
<dt>Boolean</dt>
<dd>如果指定的值(value)存在于Set对象当中,返回<code>true</code>;否则返回 <code>false</code>。</dd>
<dd>如果指定的值(value)存在于 Set 对象当中,返回<code>true</code>;否则返回 <code>false</code>。</dd>
</dl>

<h2 id="Examples">示例</h2>
Expand All @@ -47,7 +47,7 @@ <h3 id="Example:_Testing_size_of_all_array_elements">使用 <code>has</code> 方

set1.has(obj1); // 返回 true
set1.has({'key1': 1}); // 会返回 false,因为其是另一个对象的引用
set1.add({'key1': 1}); // 现在 set1 中有2条(不同引用的)对象了</pre>
set1.add({'key1': 1}); // 现在 set1 中有 2 条(不同引用的)对象了</pre>

<h2 id="规范">规范</h2>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,26 @@

<h2 id="简述">简述</h2>

<p><code>Set</code>对象是值的集合,你可以按照插入的顺序迭代它的元素。 Set中的元素只会<strong>出现一次</strong>,即 Set 中的元素是唯一的。</p>
<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>因为 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>
<p>另外,<code>NaN</code><code>undefined</code> 都可以被存储在 Set 中,<code>NaN</code> 之间被视为相同的值(NaN 被认为是相同的,尽管 NaN !== NaN)。</p>

<h2 id="Constructor">Constructor</h2>

<dl>
<dt><a href="/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Set/Set"><code>Set()</code></a></dt>
<dd>创建一个新的<code>Set</code>对象。</dd>
<dd>创建一个新的 <code>Set</code> 对象。</dd>
</dl>

<h2 id="静态属性">静态属性</h2>

<dl>
<dt>{{jsxref("Set.@@species", "get Set[@@species]")}}</dt>
<dd>构造函数用来创建派生对象.</dd>
<dd>构造函数用来创建派生对象</dd>
</dl>

<h2 id="Properties">实例属性</h2>
Expand All @@ -48,28 +48,28 @@ <h2 id="实例方法">实例方法</h2>

<dl>
<dt>{{jsxref("Set.add", "Set.prototype.add(<em>value</em>)")}}</dt>
<dd>在<code>Set</code>对象尾部添加一个元素。返回该<code>Set</code>对象。</dd>
<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>
<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>
<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>
<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>
<dd>返回一个布尔值,表示该值在 <code>Set</code> 中存在与否。</dd>
<dt>{{jsxref("Set.prototype.keys()")}}</dt>
<dd>与<strong><code>values()</code></strong>方法相同,返回一个新的迭代器对象,该对象包含<code>Set</code>对象中的按插入顺序排列的所有元素的值。</dd>
<dd>与 <strong><code>values()</code></strong> 方法相同,返回一个新的迭代器对象,该对象包含 <code>Set</code> 对象中的按插入顺序排列的所有元素的值。</dd>
<dt>{{jsxref("Set.prototype.values()")}}</dt>
<dd>返回一个新的迭代器对象,该对象包含<code>Set</code>对象中的按插入顺序排列的所有元素的值。</dd>
<dd>返回一个新的迭代器对象,该对象包含 <code>Set</code> 对象中的按插入顺序排列的所有元素的值。</dd>
<dt>{{jsxref("Set.prototype.@@iterator()", "Set.prototype[@@iterator]()")}}</dt>
<dd>返回一个新的迭代器对象,该对象包含<code>Set</code>对象中的按插入顺序排列的所有元素的值。</dd>
<dd>返回一个新的迭代器对象,该对象包含 <code>Set</code> 对象中的按插入顺序排列的所有元素的值。</dd>
</dl>

<h2 id="示例">示例</h2>

<h3 id="使用Set对象">使用<code>Set</code>对象</h3>
<h3 id="使用_Set_对象">使用 <code>Set</code> 对象</h3>

<pre class="brush: js">let mySet = new Set();

Expand All @@ -91,18 +91,18 @@ <h3 id="使用Set对象">使用<code>Set</code>对象</h3>

mySet.size; // 5

mySet.delete(5); // true, 从set中移除5
mySet.has(5); // false, 5已经被移除
mySet.delete(5); // true,从 set 中移除 5
mySet.has(5); // false, 5 已经被移除

mySet.size; // 4, 刚刚移除一个值
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>
<h3 id="迭代Set">迭代 Set</h3>

<pre class="brush: js">// 迭代整个set
<pre class="brush: js">// 迭代整个 set
// 按顺序输出:1, "some text", {"a": 1, "b": 2}, {"a": 1, "b": 2}
for (let item of mySet) console.log(item);

Expand All @@ -116,14 +116,14 @@ <h3 id="迭代Set">迭代Set</h3>
//(键与值相等)
for (let [key, value] of mySet.entries()) console.log(key);

// 使用 Array.from 转换Set为Array
// 使用 Array.from 转换 Set 为 Array
var myArr = Array.from(mySet); // [1, "some text", {"a": 1, "b": 2}, {"a": 1, "b": 2}]

// 如果在HTML文档中工作,也可以:
// 如果在 HTML 文档中工作,也可以:
mySet.add(document.body);
mySet.has(document.querySelector("body")); // true

// Set 和 Array互换
// Set 和 Array 互换
mySet2 = new Set([1, 2, 3, 4]);
mySet2.size; // 4
[...mySet2]; // [1,2,3,4]
Expand All @@ -134,7 +134,7 @@ <h3 id="迭代Set">迭代Set</h3>
// 可以通过如下代码模拟求差集
let difference = new Set([...set1].filter(x =&gt; !set2.has(x)));

// 用forEach迭代
// 用 forEach 迭代
mySet.forEach(function(value) {
console.log(value);
});
Expand Down Expand Up @@ -210,13 +210,13 @@ <h3 id="Array_相关"> <code>Array</code> 相关</h3>

<pre class="brush: js">let myArray = ["value1", "value2", "value3"];

// 用Set构造器将Array转换为Set
// 用 Set 构造器将 Array 转换为 Set
let mySet = new Set(myArray);

mySet.has("value1"); // returns true

// 用...(展开操作符)操作符将Set转换为Array
console.log([...mySet]); // 与myArray完全一致
// 用...(展开操作符) 操作符将 Set 转换为 Array
console.log([...mySet]); // 与 myArray 完全一致
</pre>

<h3 id="数组去重">数组去重</h3>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ <h2 id="语法">语法</h2>

<h2 id="描述">描述</h2>

<p><code>byteLength</code>属性是一个访问者属性,其set访问者函数为 <code>undefined</code>,这意味着您只能读取此属性。 该值在构造共享数组时建立,并且无法更改。</p>
<p><code>byteLength</code>属性是一个访问者属性,其 set 访问者函数为 <code>undefined</code>,这意味着您只能读取此属性。 该值在构造共享数组时建立,并且无法更改。</p>

<h2 id="例子">例子</h2>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ worker.postMessage(sab);

共享内存能被同时创建和更新于 `worker` 线程或主线程。依赖于系统(CPU、操作系统、浏览器),变化传递给所有上下文环境需要一段时间。因此需要通过{{jsxref("Atomics", "原子", "", 1)}}操作来进行同步。

### 接受 SharedArrayBuffer 对象的API
### 接受 SharedArrayBuffer 对象的 API

- {{domxref("WebGLRenderingContext.bufferData()")}}
- {{domxref("WebGLRenderingContext.bufferSubData()")}}
- {{domxref("WebGL2RenderingContext.getBufferSubData()")}}

### 安全需求

为应对[幽灵漏洞](https://zh.wikipedia.org/wiki/幽灵漏洞),所有主流浏览器均默认[于2018年1月5日禁用SharedArrayBuffer](https://blog.mozilla.org/security/2018/01/03/mitigations-landing-new-class-timing-attack/)。在 2020 年,一种新的、安全的方法已经标准化,以重新启用 `SharedArrayBuffer`。通过一些安全措施,[`postMessage()`](/zh-CN/docs/Web/API/Window/postMessage) 将不再抛出有关 `SharedArrayBuffer` 对象的异常,跨线程共享内存也变得可用:
为应对[幽灵漏洞](https://zh.wikipedia.org/wiki/幽灵漏洞),所有主流浏览器均默认[于 2018 年 1 月 5 日禁用 SharedArrayBuffer](https://blog.mozilla.org/security/2018/01/03/mitigations-landing-new-class-timing-attack/)。在 2020 年,一种新的、安全的方法已经标准化,以重新启用 `SharedArrayBuffer`。通过一些安全措施,[`postMessage()`](/zh-CN/docs/Web/API/Window/postMessage) 将不再抛出有关 `SharedArrayBuffer` 对象的异常,跨线程共享内存也变得可用:

作为基准需求,你的文档需处于[安全上下文](/zh-CN/docs/Web/Security/Secure_Contexts)中。

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
---
<div>{{JSRef}}</div>

<p><code><strong>SharedArrayBuffer.prototype.slice()</strong></code> 方法返回一个新的{{jsxref("SharedArrayBuffer")}} 副本,其内容是<code>该SharedArrayBuffer</code>的字节从<code>begin</code>开始(包含<code>begin</code>),直到<code>end</code>结束(不包含<code>end</code>)。如果<code>begin</code>或<code>end</code>是负的,它指的是从数组末尾开始的索引。此方法与 {{jsxref("Array.prototype.slice()")}} 具有相同的算法。</p>
<p><code><strong>SharedArrayBuffer.prototype.slice()</strong></code> 方法返回一个新的{{jsxref("SharedArrayBuffer")}} 副本,其内容是<code>该 SharedArrayBuffer</code>的字节从<code>begin</code>开始 (包含<code>begin</code>),直到<code>end</code>结束 (不包含<code>end</code>)。如果<code>begin</code>或<code>end</code>是负的,它指的是从数组末尾开始的索引。此方法与 {{jsxref("Array.prototype.slice()")}} 具有相同的算法。</p>

<p>{{EmbedInteractiveExample("pages/js/sharedarraybuffer-slice.html")}}</p>

Expand All @@ -22,9 +22,9 @@ <h3 id="参数">参数</h3>

<dl>
<dt><code>begin</code> {{optional_inline}}</dt>
<dd>从零开始的索引,从该索引开始提取。可以使用一个负索引,表示从序列末尾开始的偏移量。 <code>slice(-2)</code>提取序列中的最后两个元素。If <code>begin</code> is undefined, <code>slice</code> begins from index <code>0</code>.如果<code>begin</code>为undefined,<code>slice</code>则从索引为<code>0</code>处开始。</dd>
<dd>从零开始的索引,从该索引开始提取。可以使用一个负索引,表示从序列末尾开始的偏移量。 <code>slice(-2)</code>提取序列中的最后两个元素。If <code>begin</code> is undefined, <code>slice</code> begins from index <code>0</code>.如果<code>begin</code>为 undefined,<code>slice</code>则从索引为<code>0</code>处开始。</dd>
<dt><code>end</code> {{optional_inline}}</dt>
<dd>从零开始的索引,在此索引<em>之前</em>终止提取。 <code>slice</code> 执行提取到索引为<code>end</code>的位置(不包含<code>end</code>)。例如,<code>slice(1,4)</code>提取第二个元素到第四个元素(索引为1、2和3的元素)。可以使用一个负索引,表示从序列末尾开始的偏移量。 <code>slice(2,-1)</code>提取序列中从第三个元素开始,到倒数第二个元素结束的全部元素。如果省略<code>end</code>,则<code>slice</code>一直提取到序列的末尾(<code>sab.byteLength)。</code></dd>
<dd>从零开始的索引,在此索引<em>之前</em>终止提取。 <code>slice</code> 执行提取到索引为<code>end</code>的位置 (不包含<code>end</code>)。例如,<code>slice(1,4)</code>提取第二个元素到第四个元素(索引为 1、2 和 3 的元素)。可以使用一个负索引,表示从序列末尾开始的偏移量。 <code>slice(2,-1)</code>提取序列中从第三个元素开始,到倒数第二个元素结束的全部元素。如果省略<code>end</code>,则<code>slice</code>一直提取到序列的末尾(<code>sab.byteLength)。</code></dd>
</dl>

<h3 id="返回值">返回值</h3>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
---
<div>{{JSRef}}</div>

<p><strong><code>[@@iterator]()</code></strong> 方法返回一个新的Iterator对象,它遍历字符串的代码点,返回每一个代码点的字符串值。</p>
<p><strong><code>[@@iterator]()</code></strong> 方法返回一个新的 Iterator 对象,它遍历字符串的代码点,返回每一个代码点的字符串值。</p>

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

Expand All @@ -17,11 +17,11 @@ <h2 id="语法">语法</h2>

<h3 id="返回值">返回值</h3>

<p>一个新的Iterator对象。</p>
<p>一个新的 Iterator 对象。</p>

<h2 id="示例">示例</h2>

<h3 id="使用iterator"><code>使用[@@iterator]()</code></h3>
<h3 id="使用iterator"><code>使用 [@@iterator]()</code></h3>

<pre class="brush:js">var string = 'A\uD835\uDC68';

Expand All @@ -31,7 +31,7 @@ <h3 id="使用iterator"><code>使用[@@iterator]()</code></h3>
console.log(strIter.next().value); // "\uD835\uDC68"
</pre>

<h3 id="通过_for..of_使用iterator"><code>通过</code> <code>for..of 使用[@@iterator]()</code></h3>
<h3 id="通过_for..of_使用iterator"><code>通过</code> <code>for..of 使用 [@@iterator]()</code></h3>

<pre class="brush:js">var string = 'A\uD835\uDC68B\uD835\uDC69C\uD835\uDC6A';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ <h3 id="参数">参数</h3>

<h3 id="返回值">返回值</h3>

<p> 包含 {{HTMLElement("a")}} HTML元素的一个字符串。</p>
<p> 包含 {{HTMLElement("a")}} HTML 元素的一个字符串。</p>

<h2 id="描述">描述</h2>

Expand Down
Loading