--- title: Array.prototype.copyWithin() slug: Web/JavaScript/Reference/Global_Objects/Array/copyWithin tags: - Array - ECMAScript 2015 - JavaScript - Prototipo - Referencia - metodo - polyfill translation_of: Web/JavaScript/Reference/Global_Objects/Array/copyWithin original_slug: Web/JavaScript/Referencia/Objetos_globales/Array/copyWithin ---
El método copyWithin() transfiere una copia plana de una sección a otra dentro del mismo array ( o contexto similar ), sin modificar su propiedad length y lo devuelve.
{{EmbedInteractiveExample("pages/js/array-copywithin.html")}}
arr.copyWithin(target) arr.copyWithin(target, start) arr.copyWithin(target, start, end)
targettarget se contará desde el final. -1 es el último elemento, -2 el penúltimo, etc.target es igual o mayor que arr.length, no se copiará nada. Si target es posicionado después de start, la secuencia copiada se recortará para que encaje con arr.length.start {{optional_inline}}start es omitido, copyWithin copiará desde el principio (por defecto es 0).end {{optional_inline}}copyWithin copiará hasta pero sin incluir el end. Si es negativo, end será contado desde el final.end es omitido, copyWithin copiará hasta el final ( por defecto es arr.length ).El array modificado.
copyWithin es similar a la función memmove de C y C++ , siendo altamente eficiente para desplazar los datos en un {{jsxref("Array")}} o {{jsxref("TypedArray")}}. La secuencia de datos es leída y escrita en una sola operación; la escritura será correcta incluso en el caso de que la zona de lectura y el destino de escritura se solapen.
La función copyWithin es intencionadamente genérica, permitiendo que se aplique en contextos en los cuales this no sea necesariamente un objeto {{jsxref("Array")}}.
El método copyWithin es un método mutador. No altera la propiedad length de this, pero cambiará su contenido y creará nuevas propiedades si es necesario.
En los siguientes ejemplos céntrate en los siguientes aspectos:
start y end trabajan juntos para decidir qué se copiará. Siempre tienen valor por defecto aunque omitas end, o start y end.target trabaja solo y debe especificarse. Indica el lugar para en el que la copia comenzará a sobreescribir datos existentes. Debe estar dentro de los límites en el contexto que se aplique.arr.copyWithin( n ) es lo mismo que arr.copyWithin( n, 0, arr.length)[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(-2, -3, -1); // [1, 2, 3, 3, 4]
A continuación se aplica en el contexto de un objeto array-like:
copyWithin es un método mutador. ¿Por qué se creó esta nueva propiedad? porque mediante el argumento target se especificó que la copia debía comenzar a partir de un índice que ¡¡no existía!![].copyWithin.call({length: 5, 3: 1}, 0, 3);
// {0: 1, 3: 1, length: 5}
Lo que sigue ahora son las subclases tipadas de Array en ES6:
// Arrays tipados en ES6. Son subclases de Array var i32a = new Int32Array([1, 2, 3, 4, 5]); i32a.copyWithin(0, 2); // Int32Array [3, 4, 5, 4, 5] // En plataformas que todavía no siguen la norma ES6: [].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 =
// Array: Number[, Number[, Number]]
function copyWithin(target, start, stop) {
var positiveT = target >= 0,
positiveS = (start = start | 0) >= 0,
length = this.length,
zero = 0,
r = function() {return ((+new Date) * Math.random()).toString(36)},
delimiter = "\b" + r() + "-" + r() + "-" + r() + "\b",
hold;
stop = stop || this.length;
hold = this.slice.apply(this,
positiveT?
[start, stop]:
positiveS?
[start, -target]:
[start])
.join(delimiter);
return this.splice.apply(this,
positiveT?
[target, stop - start, hold]:
positiveS?
[target, stop, hold]:
[target, start, hold]),
this.join(delimiter).split(delimiter).slice(zero, length);
}
}
| Especificación | Estado | Comentario |
|---|---|---|
| {{SpecName('ES6', '#sec-array.prototype.copyWithin', 'Array.prototype.copyWithin')}} | {{Spec2('ES6')}} | Definición inicial. |
| {{SpecName('ESDraft', '#sec-array.prototype.copyWithin', 'Array.prototype.copyWithin')}} | {{Spec2('ESDraft')}} |
{{Compat("javascript.builtins.Array.copyWithin")}}