TypedArray.prototype.copyWithin()
Baseline
広く利用可能
この機能は広く実装されており、多くのバージョンの端末やブラウザーで動作します。2016年9月以降、すべてのブラウザーで利用可能です。
copyWithin() は TypedArray インスタンスのメソッドで、この型付き配列の一部を同じ型付き配列の別の場所にシャローコピーし、この型付き配列の長さを変更せずに返します。このメソッドは Array.prototype.copyWithin() と同じアルゴリズムです。
試してみましょう
const uint8 = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]);
// 挿入位置、開始位置、終了位置
uint8.copyWithin(3, 1, 3);
console.log(uint8);
// 予想される結果: Uint8Array [1, 2, 3, 2, 3, 6, 7, 8]
構文
js
copyWithin(target, start)
copyWithin(target, start, end)
引数
返値
変更された型付き配列です。
解説
詳細については、 Array.prototype.copyWithin() をご覧ください。このメソッドは汎用的ではなく、型付き配列インスタンスに対してのみ呼び出すことができます。
例
>copyWithin() の使用
js
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 ]
仕様書
| 仕様書 |
|---|
| ECMAScript® 2027 Language Specification> # sec-%typedarray%.prototype.copywithin> |