[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array/copyWithin [Back]  [Original]

Array.prototype.copyWithin() - JavaScript | MDN

This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

Array.prototype.copyWithin()

Baseline Widely available

This feature is well established and works across many devices and browser versions. Its been available across browsers since 2015 9.

Array copyWithin() , .

In this article

const array1 = ["a", "b", "c", "d", "e"];

// Copy to index 0 the element at index 3
console.log(array1.copyWithin(0, 3, 4));
// Expected output: Array ["d", "b", "c", "d", "e"]

// Copy to index 1 all elements from index 3 to the end
console.log(array1.copyWithin(1, 3));
// Expected output: Array ["d", "d", "e", "d", "e"]

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

target

0 , . start , start end .

  • . -array.length <= target < 0, target + array.length .
  • target < -array.length, 0 .
  • target >= array.length, .
  • target start , array.length (, copyWithin() ).
start

0 , .

  • . -array.length <= start < 0, target + array.length .
  • target < -array.length, 0 .
  • target >= array.length, .
end Optional

0 , . copyWithin() end end .

  • . -array.length <= end < 0, target + array.length .
  • target < -array.length, 0 .
  • end >= array.length end , array.length . .
  • end start .

.

copyWithin() C C++ memmove , Array . TypedArray . . .

undefined 0 , start 0 target . . 0 start .

js
console.log([1, 2, 3, 4, 5].copyWithin(2));
// [1, 2, 1, 2, 3];    2 .

copyWithin() . this , this , .

copyWithin() . , .

copyWithin() . this length . , .

copyWithin()

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

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

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

copyWithin()

copyWithin() .

js
console.log([1, , 3].copyWithin(2, 1, 2)); // [1, empty, empty]

copyWithin()

copyWithin() this length .

js
const arrayLike = {
  length: 5,
  3: 1,
};
console.log(Array.prototype.copyWithin.call(arrayLike, 0, 3));
// { '0': 1, '3': 1, length: 5 }
console.log(Array.prototype.copyWithin.call(arrayLike, 3, 1));
// { '0': 1, length: 5 }
//     '3'  .

Specification
ECMAScript 2027 LanguageSpecification
# sec-array.prototype.copywithin


Web Proxy Viewer  |  New URL  |  Original Page