TypedArray.prototype.copyWithin()

copyWithin() メソッドは、配列内の一連の配列の要素を target から始まる位置にコピーします。コピーは第 2、第 3の引数、 startend の位置から実施されます。 end 引数はオプションで、既定では配列の長さです。このメソッドは Array.prototype.copyWithin と同じアルゴリズムです。 TypedArray は、ここでは 型付き配列型のうちの一つです。

試してみましょう

構文

copyWithin(target, start)
copyWithin(target, start, end)

引数

target

要素をコピーする対象の開始インデックス位置。

start

要素をコピーし始める元の開始インデックス位置。

end 省略可

オプション。要素をコピーし終わる元の終了インデックス位置。

返値

変更された配列です。

解説

詳細については、 Array.prototype.copyWithin をご覧ください

copyWithin の使用

const buffer = new ArrayBuffer(8);
const uint8 = new Uint8Array(buffer);
uint8.set([1, 2, 3]);
console.log(uint8); // Uint8Array [ 1, 2, 3, 0, 0, 0, 0, 0 ]
uint8.copyWithin(3, 0, 3);
console.log(uint8); // Uint8Array [ 1, 2, 3, 1, 2, 3, 0, 0 ]

仕様書

Specification
ECMAScript Language Specification
# sec-%typedarray%.prototype.copywithin

ブラウザーの互換性

BCD tables only load in the browser

関連情報