Movatterモバイル変換


[0]ホーム

URL:


  1. 開発者向けのウェブ技術
  2. JavaScript
  3. JavaScript リファレンス
  4. 標準組み込みオブジェクト
  5. TypedArray
  6. TypedArray.prototype.copyWithin()

このページはコミュニティーの尽力で英語から翻訳されました。MDN Web Docsコミュニティーについてもっと知り、仲間になるにはこちらから。

View in EnglishAlways switch to English

TypedArray.prototype.copyWithin()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨2016年9月⁩.

copyWithin()TypedArray インスタンスのメソッドで、この型付き配列の一部を同じ型付き配列の別の場所にシャローコピーし、この型付き配列の長さを変更せずに返します。このメソッドはArray.prototype.copyWithin() と同じアルゴリズムです。

試してみましょう

const uint8 = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]);// Insert position, start position, end positionuint8.copyWithin(3, 1, 3);console.log(uint8);// Expected output: Uint8Array [1, 2, 3, 2, 3, 6, 7, 8]

構文

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

引数

target

並びのコピー先となるゼロ基点のインデックスで、整数に変換されます。これはstart にある要素がコピーされる場所に対応し、start からend までのすべての要素が後続のインデックスにコピーされます。

start

コピー元でコピーを始める位置のゼロ基点のインデックスで、整数に変換されます

end省略可

コピー元でコピーを終える位置のゼロ基点のインデックスで、整数に変換されますcopyWithin() はここまでをコピーしますが、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 ]

仕様書

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

ブラウザーの互換性

関連情報

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp