Array.prototype.copyWithin()
copyWithin()
メソッドは、サイズを変更せずに、配列の一部を同じ配列内の別の場所にシャローコピーして返します。
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
構文
arr.copyWithin(target[, start[, end]])
引数
target
- シーケンスのコピー先となる、0 から始まるインデックスです。負の場合、
target
は最後から数えられます。 target
がarr.length
以上の場合、何もコピーされません。target
がstart
より後に配置されている場合、コピーされたシーケンスはarr.length
に合うように切り詰められます。start
省略可- 要素のコピー元の始まりを表す、0 から始まるインデックスです。負の場合、
start
は最後から数えられます。 start
が省略された場合、copyWithin
は最初の要素からコピーします(つまり、初期値は 0 です)。end
省略可- 要素のコピー元の末尾を表す、0 から始まるインデックスです。
copyWithin
のコピーはend
を含みません。負の場合、end
は最後から数えられます。 end
が省略された場合、copyWithin
は最後までコピーします(つまり、初期値はarr.length
です)。
戻り値
変更された配列です。
説明
copyWithin
メソッドは C 言語や C++ 言語の memmove
のような動きをし、Array
のデータをシフトさせる高いパフォーマンスのメソッドです。これは特に TypedArray
の同名メソッドに当てはまります。シーケンスはコピーされ貼り付けられる処理が一命令で行われます。コピー&ペースト領域が重なっている場合でも、ペーストされたシーケンスはコピーされた値を持ちます。
copyWithin
関数は、ジェネリック関数として動作します。this
値が Array
オブジェクトである必要はありません。
copyWithin
は可変メソッドであり、this
オブジェクト自身を変更し、コピーではなく、オブジェクト自身を返します。
ポリフィル
if (!Array.prototype.copyWithin) {
Object.defineProperty(Array.prototype, 'copyWithin', {
value: 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;
},
configurable: true,
writable: true
});
}
例
copyWithin の使用
[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}
// ES2015 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 ES2015 compliant:
[].copyWithin.call(new Int32Array([1, 2, 3, 4, 5]), 0, 3, 4);
// Int32Array [4, 2, 3, 4, 5]
仕様
ブラウザー実装状況
BCD tables only load in the browser