Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Array.prototype.copyWithin()

copyWithin() メソッドは、配列内の配列要素の並びを target で始まる位置にコピーします。このコピーは、2, 3 番目の引数である startend に指定したインデックス位置から取られます。end 引数の指定は任意であり、デフォルトで配列の長さ (最後尾) になります。

構文

arr.copyWithin(target, start[, end = this.length])

引数

target
要素コピー先の開始インデックス位置
start
要素コピー元の先頭インデックス位置
end
任意。要素コピー元の末尾インデックス位置

説明

copyWithinメソッドはC言語やCpp言語のmemcpyのような動きをし、 Array or TypedArray をシフトさせるときには高いパフォーマンスを期待することができる。シーケンスはコピーされて貼り付けられる。(訳注:そのため参照渡しのような動作になります(シャローコピー))

引数 target および start, end は、Number 型に強制変換され、整数値に丸められます。

start が負の値の場合、length+start として扱われます。length は、配列の要素数です。end が負の値の場合、length+end として扱われます。

copyWithin 関数は、generic な関数として動作します。this 値が Array オブジェクトである必要はありません。

copyWithinmutable メソッドであり、this オブジェクト自身を変更し、コピーではなく、オブジェクト自身を返します。

[1, 2, 3, 4, 5].copyWithin(-2);
// [1, 2, 3, 1, 2]

[1, 2, 3, 4, 5].copyWithin(0, 3);
// [4, 5, 3, 4, 5]

[1, 2, 3, 4, 5].copyWithin(0, 3, 4);
// [4, 2, 3, 4, 5]

[1, 2, 3, 4, 5].copyWithin(0, -2, -1);
// [4, 2, 3, 4, 5]

[].copyWithin.call({length: 5, 3: 1}, 0, 3);
// {0: 1, 3: 1, length: 5}

// ES6 Typed Arrays are subclasses of Array
var i32a = new Int32Array([1, 2, 3, 4, 5]);

i32a.copyWithin(0, 2);
// Int32Array [3, 4, 5, 4, 5]

// On platforms that are not yet ES6 compliant: 
[].copyWithin.call(new Int32Array([1, 2, 3, 4, 5]), 0, 3, 4);
// Int32Array [4, 2, 3, 4, 5]

互換コード

if (!Array.prototype.copyWithin) {
  Array.prototype.copyWithin = function(target, start/*, end*/) {
    // Steps 1-2.
    if (this == null) {
      throw new TypeError('this is null or not defined');
    }

    var O = Object(this);

    // Steps 3-5.
    var len = O.length >>> 0;

    // Steps 6-8.
    var relativeTarget = target >> 0;

    var to = relativeTarget < 0 ?
      Math.max(len + relativeTarget, 0) :
      Math.min(relativeTarget, len);

    // Steps 9-11.
    var relativeStart = start >> 0;

    var from = relativeStart < 0 ?
      Math.max(len + relativeStart, 0) :
      Math.min(relativeStart, len);

    // Steps 12-14.
    var end = arguments[2];
    var relativeEnd = end === undefined ? len : end >> 0;

    var final = relativeEnd < 0 ?
      Math.max(len + relativeEnd, 0) :
      Math.min(relativeEnd, len);

    // Step 15.
    var count = Math.min(final - from, len - to);

    // Steps 16-17.
    var direction = 1;

    if (from < to && to < (from + count)) {
      direction = -1;
      from += count - 1;
      to += count - 1;
    }

    // Step 18.
    while (count > 0) {
      if (from in O) {
        O[to] = O[from];
      } else {
        delete O[to];
      }

      from += direction;
      to += direction;
      count--;
    }

    // Step 19.
    return O;
  };
}

仕様

仕様 ステータス コメント
ECMAScript 2015 (6th Edition, ECMA-262)
Array.prototype.copyWithin の定義
標準 Initial definition.
ECMAScript 2017 Draft (ECMA-262)
Array.prototype.copyWithin の定義
ドラフト  

ブラウザ実装状況

機能 Chrome Firefox (Gecko) Internet Explorer Opera Safari
基本サポート 45 32 (32) 未サポート 未サポート 未サポート
機能 Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
基本サポート 未サポート 未サポート 32.0 (32) 未サポート 未サポート 未サポート

関連情報

ドキュメントのタグと貢献者

 このページの貢献者: lv7777, Marsf, shide55
 最終更新者: lv7777,