aboutsummaryrefslogtreecommitdiff
path: root/files/pt-br/web/javascript/reference/global_objects/array/copywithin/index.html
diff options
context:
space:
mode:
Diffstat (limited to 'files/pt-br/web/javascript/reference/global_objects/array/copywithin/index.html')
-rw-r--r--files/pt-br/web/javascript/reference/global_objects/array/copywithin/index.html203
1 files changed, 203 insertions, 0 deletions
diff --git a/files/pt-br/web/javascript/reference/global_objects/array/copywithin/index.html b/files/pt-br/web/javascript/reference/global_objects/array/copywithin/index.html
new file mode 100644
index 0000000000..4802e87b94
--- /dev/null
+++ b/files/pt-br/web/javascript/reference/global_objects/array/copywithin/index.html
@@ -0,0 +1,203 @@
+---
+title: Array.prototype.copyWithin()
+slug: Web/JavaScript/Reference/Global_Objects/Array/copyWithin
+translation_of: Web/JavaScript/Reference/Global_Objects/Array/copyWithin
+---
+<div>{{JSRef}}</div>
+
+<p>O método <code><strong>copyWithin()</strong></code> copia parte de um array para outra localização dentro deste mesmo array e o retorna, sem alterar seu tamanho. </p>
+
+<h2 id="Sintaxe">Sintaxe</h2>
+
+<pre class="syntaxbox"><code><var>arr</var>.copyWithin(<var>target</var>, <var>start</var>[, <var>end</var> = this.length])</code></pre>
+
+<h3 id="Parâmetros">Parâmetros</h3>
+
+<dl>
+ <dt><code>target</code></dt>
+ <dd>Posição para a qual os elementos serão copiados. Caso negativo, o <code>target</code> será contado a partir do final.</dd>
+ <dt><code>start</code></dt>
+ <dd>Índice inicial de onde se copiará os elementos. Caso negativo, o <code>start</code> será contado a partir do final.</dd>
+ <dt><code>end </code>{{optional_inline}}</dt>
+ <dd>Índice final de onde se copiará os elementos. Caso negativo, o <code>end</code> será contado a partir do final.</dd>
+</dl>
+
+<h2 id="Descrição">Descrição</h2>
+
+<p>Os argumentos <code>target</code>, <code>start</code> e <code>end</code> são restritos a {{jsxref("Number")}} e truncados para valores inteiros.</p>
+
+<p>Se <code>start</code> for negativo, ele é tratado como <code>length+start</code>, onde <code>length</code> é o comprimento do array. Se <code>end</code> for negativo, ele é tratado como <code>length+end</code>.</p>
+
+<p>A função <code>copyWithin</code> é intencionalmente <em>genérica</em>, não requer que seu valor <code>this</code> seja um objeto {{jsxref("Array")}} e, adicionalmente, <code>copyWithin</code> é um <em>método mutável</em>, irá mudar o próprio objeto <code>this</code> e retorná-lo, não apenas retornar uma cópia dele.</p>
+
+<h2 id="Exemplos">Exemplos</h2>
+
+<pre class="brush: js">[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}
+
+// Typed Arrays do ES6 são subclasses de Array
+var i32a = new Int32Array([1, 2, 3, 4, 5]);
+
+i32a.copyWithin(0, 2);
+// Int32Array [3, 4, 5, 4, 5]
+
+// Em plataformas que ainda não são compatíveis com ES6:
+[].copyWithin.call(new Int32Array([1, 2, 3, 4, 5]), 0, 3, 4);
+// Int32Array [4, 2, 3, 4, 5]
+</pre>
+
+<h2 id="Polyfill">Polyfill</h2>
+
+<pre class="brush: js">if (!Array.prototype.copyWithin) {
+ Array.prototype.copyWithin = function(target, start/*, end*/) {
+ // Passos 1-2.
+ if (this == null) {
+ throw new TypeError('this é null ou não definido');
+ }
+
+ var O = Object(this);
+
+ // Passos 3-5.
+ var len = O.length &gt;&gt;&gt; 0;
+
+ // Passos 6-8.
+ var relativeTarget = target &gt;&gt; 0;
+
+ var to = relativeTarget &lt; 0 ?
+ Math.max(len + relativeTarget, 0) :
+ Math.min(relativeTarget, len);
+
+ // Passos 9-11.
+ var relativeStart = start &gt;&gt; 0;
+
+ var from = relativeStart &lt; 0 ?
+ Math.max(len + relativeStart, 0) :
+ Math.min(relativeStart, len);
+
+ // Passos 12-14.
+ var end = arguments[2];
+ var relativeEnd = end === undefined ? len : end &gt;&gt; 0;
+
+ var final = relativeEnd &lt; 0 ?
+ Math.max(len + relativeEnd, 0) :
+ Math.min(relativeEnd, len);
+
+ // Passo 15.
+ var count = Math.min(final - from, len - to);
+
+ // Passos 16-17.
+ var direction = 1;
+
+ if (from &lt; to &amp;&amp; to &lt; (from + count)) {
+ direction = -1;
+ from += count - 1;
+ to += count - 1;
+ }
+
+ // Passo 18.
+ while (count &gt; 0) {
+ if (from in O) {
+ O[to] = O[from];
+ } else {
+ delete O[to];
+ }
+
+ from += direction;
+ to += direction;
+ count--;
+ }
+
+ // Passo 19.
+ return O;
+ };
+}
+</pre>
+
+<h2 id="Especificações">Especificações</h2>
+
+<table class="standard-table">
+ <tbody>
+ <tr>
+ <th scope="col">Especificação</th>
+ <th scope="col">Status</th>
+ <th scope="col">Comentário</th>
+ </tr>
+ <tr>
+ <td>{{SpecName('ES6', '#sec-array.prototype.copyWithin', 'Array.prototype.copyWithin')}}</td>
+ <td>{{Spec2('ES6')}}</td>
+ <td>Definição inicial.</td>
+ </tr>
+ <tr>
+ <td>{{SpecName('ESDraft', '#sec-array.prototype.copyWithin', 'Array.prototype.copyWithin')}}</td>
+ <td>{{Spec2('ESDraft')}}</td>
+ <td> </td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="Compatibilidade_de_navegadores">Compatibilidade de navegadores</h2>
+
+<div>{{CompatibilityTable}}</div>
+
+<div id="compat-desktop">
+<table class="compat-table">
+ <tbody>
+ <tr>
+ <th>Feature</th>
+ <th>Chrome</th>
+ <th>Firefox (Gecko)</th>
+ <th>Internet Explorer</th>
+ <th>Opera</th>
+ <th>Safari</th>
+ </tr>
+ <tr>
+ <td>Suporte básico</td>
+ <td>{{CompatChrome("45")}}</td>
+ <td>{{CompatGeckoDesktop("32")}}</td>
+ <td>{{CompatNo}}</td>
+ <td>{{CompatNo}}</td>
+ <td>{{CompatNo}}</td>
+ </tr>
+ </tbody>
+</table>
+</div>
+
+<div id="compat-mobile">
+<table class="compat-table">
+ <tbody>
+ <tr>
+ <th>Feature</th>
+ <th>Android</th>
+ <th>Chrome for Android</th>
+ <th>Firefox Mobile (Gecko)</th>
+ <th>IE Mobile</th>
+ <th>Opera Mobile</th>
+ <th>Safari Mobile</th>
+ </tr>
+ <tr>
+ <td>Suporte básico</td>
+ <td>{{CompatNo}}</td>
+ <td>{{CompatNo}}</td>
+ <td>{{CompatGeckoMobile("32")}}</td>
+ <td>{{CompatNo}}</td>
+ <td>{{CompatNo}}</td>
+ <td>{{CompatNo}}</td>
+ </tr>
+ </tbody>
+</table>
+</div>
+
+<h2 id="Veja_também">Veja também</h2>
+
+<ul>
+ <li>{{jsxref("Array")}}</li>
+</ul>