TypedArray.prototype.copyWithin()
En esta página
The copyWithin() method of {{jsxref("TypedArray")}} instances shallow copies part of this typed array to another location in the same typed array and returns this typed array without modifying its length. This method has the same algorithm as {{jsxref("Array.prototype.copyWithin()")}}.
{{InteractiveExample("JavaScript Demo: TypedArray.prototype.copyWithin()")}}
```js interactive-example const uint8 = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]);
// Insert position, start position, end position uint8.copyWithin(3, 1, 3);
console.log(uint8); // Expected output: Uint8Array [1, 2, 3, 2, 3, 6, 7, 8]
## Syntax
```js-nolint
copyWithin(target, start)
copyWithin(target, start, end)
Parameters#
target- : Zero-based index at which to copy the sequence to, converted to an integer. This corresponds to where the element at
startwill be copied to, and all elements betweenstartandendare copied to succeeding indices. start- : Zero-based index at which to start copying elements from, converted to an integer.
end{{optional_inline}}- : Zero-based index at which to end copying elements from, converted to an integer.
copyWithin()copies up to but not includingend.
Return value#
The modified typed array.
Description#
See {{jsxref("Array.prototype.copyWithin()")}} for more details. This method is not generic and can only be called on typed array instances.
Examples#
Using copyWithin()#
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 ]
Specifications#
{{Specifications}}
Browser compatibility#
{{Compat}}
See also#
- Polyfill of
TypedArray.prototype.copyWithinincore-js - JavaScript typed arrays guide
- {{jsxref("TypedArray")}}
- {{jsxref("Array.prototype.copyWithin()")}}