aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/global_objects/sharedarraybuffer/slice/index.html
blob: 59937c30c1cf6fe68ad4bae3cb977f4f4c334d41 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
---
title: SharedArrayBuffer.prototype.slice()
slug: Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer/slice
tags:
  - SharedArrayBuffer
  - slice
translation_of: Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer/slice
---
<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>{{EmbedInteractiveExample("pages/js/sharedarraybuffer-slice.html")}}</p>

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

<pre class="syntaxbox"><var>sab</var>.slice()
<var>sab</var>.slice(begin)
<var>sab</var>.slice(begin, end)</pre>

<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>
 <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>
</dl>

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

<p>一个包含被提取出的元素的新 {{jsxref("SharedArrayBuffer")}}</p>

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

<pre class="brush:js">var sab = new SharedArrayBuffer(1024);
sab.slice();    // SharedArrayBuffer { byteLength: 1024 }
sab.slice(2);   // SharedArrayBuffer { byteLength: 1022 }
sab.slice(-2);  // SharedArrayBuffer { byteLength: 2 }
sab.slice(0, 1); // SharedArrayBuffer { byteLength: 1 }
</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('ESDraft', '#sec-sharedarraybuffer.prototype.slice', 'SharedArrayBuffer.prototype.slice')}}</td>
   <td>{{Spec2('ESDraft')}}</td>
   <td>Initial definition in ES2017.</td>
  </tr>
 </tbody>
</table>

<h2 id="浏览器兼容性">浏览器兼容性</h2>



<p>{{Compat("javascript.builtins.SharedArrayBuffer.slice")}}</p>

<h2 id="相关链接">相关链接</h2>

<ul>
 <li>{{jsxref("SharedArrayBuffer")}}</li>
 <li>{{jsxref("Array.prototype.slice()")}}</li>
</ul>