ArrayBuffer.prototype.slice()
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
slice()
メソッドは新しい ArrayBuffer
を返し、その中にこの ArrayBuffer
の begin
から end
の手前までをコピーします。
試してみましょう
// Create an ArrayBuffer with a size in bytes
const buffer = new ArrayBuffer(16);
const int32View = new Int32Array(buffer);
// Produces Int32Array [0, 0, 0, 0]
int32View[1] = 42;
const sliced = new Int32Array(buffer.slice(4, 12));
// Produces Int32Array [42, 0]
console.log(sliced[0]);
// Expected output: 42
構文
js
slice(begin)
slice(begin, end)
引数
返値
新しい ArrayBuffer
オブジェクト。
解説
slice()
メソッドは、end
引数で指定されたバイトの手前までコピーを行います。begin
または end
のどちらかが負の数の場合は、開始位置とは反対に、配列の末尾からのインデックスを参照します。
begin
および end
引数で指定された範囲は、現在の配列で妥当なインデックスの範囲に丸められます。新しい ArrayBuffer
の計算された長さが負の数であった場合、ゼロに丸められます。
例
ArrayBuffer のコピー
js
const buf1 = new ArrayBuffer(8);
const buf2 = buf1.slice(0);
仕様書
Specification |
---|
ECMAScript® 2025 Language Specification # sec-arraybuffer.prototype.slice |
ブラウザーの互換性
Report problems with this compatibility data on GitHubdesktop | mobile | server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
slice |
Legend
Tip: you can click/tap on a cell for more information.
- Full support
- Full support
- See implementation notes.
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.