aboutsummaryrefslogtreecommitdiff
path: root/files/pt-pt/web/javascript/reference/functions/arguments/index.html
blob: 01c656de888b8139478be421194864afab78cb92 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
---
title: O objeto arguments
slug: Web/JavaScript/Reference/Functions/arguments
tags:
  - Class
  - Funções
  - JavaScript
  - Referencia
  - arguments
translation_of: Web/JavaScript/Reference/Functions/arguments
original_slug: Web/JavaScript/Reference/Funcoes/arguments
---
<p>{{JSSidebar("Functions")}}</p>

<p><strong><code>arguments</code></strong> é um objeto semelhante a uma "<code>Matiz</code>" acessível dentro de <em>functions </em>que contém os valores dos argumentos passados ​​para essa função.</p>

<div class="blockIndicator note">
<p><strong>Nota:</strong> If you're writing ES6 compatible code, then <a href="/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters">rest parameters</a> should be preferred.</p>
</div>

<div class="blockIndicator note">
<p><strong>Nota:</strong> “Array-like” means that <code>arguments</code> has a <code><a href="/en-US/docs/Web/JavaScript/Reference/Functions/arguments/length">length</a></code> property and properties indexed from zero, but it doesn't have {{JSxRef("Array")}}'s built-in methods like <code><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach">forEach</a></code> and <code><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map">map</a></code>. See <a href="#Description">§Description</a> for details.</p>
</div>

<div>{{EmbedInteractiveExample("pages/js/functions-arguments.html")}}</div>



<h2 id="Sintaxe">Sintaxe</h2>

<pre class="syntaxbox">arguments</pre>

<h2 id="Descrição">Descrição</h2>

<p>The <code>arguments</code> object is a local variable available within all non-<a href="/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions">arrow</a> functions. You can refer to a function's arguments inside that function by using its <code>arguments</code> object. It has entries for each argument the function was called with, with the first entry's index at 0.</p>

<p>For example, if a function is passed 3 arguments, you can access them as follows:</p>

<pre class="brush: js">arguments[0] // first argument
arguments[1] // second argument
arguments[2] // third argument
</pre>

<p>Each argument can also be set or reassigned:</p>

<pre class="brush: js">arguments[1] = 'new value';
</pre>

<p>The <code>arguments</code> object is not an {{JSxRef("Array")}}. It is similar, but does not have any <code>Array</code> properties except <code><a href="/en-US/docs/Web/JavaScript/Reference/Functions/arguments/length">length</a></code>. For example, it does not have the <code><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/pop">pop</a></code> method. However, it can be converted to a real <code>Array</code>:</p>

<pre class="brush: js">var args = Array.prototype.slice.call(arguments);
// Using an array literal is shorter than above but allocates an empty array
var args = [].slice.call(arguments);
</pre>

<p>As you can do with any Array-like object, you can use ES2015's {{JSxRef("Array.from()")}} method or <a href="/en-US/docs/Web/JavaScript/Reference/Operators/Spread_operator">spread operator</a> to convert <code>arguments</code> to a real Array:</p>

<pre>let args = Array.from(arguments);
// or
let args = [...arguments];</pre>

<p>The <code>arguments</code> object is useful for functions called with more arguments than they are formally declared to accept. This technique is useful for functions that can be passed a variable number of arguments, such as <code><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/min">Math.min()</a></code>. This example function accepts any number of string arguments and returns the longest one:</p>

<pre class="brush: js">function longestString() {
  var longest = '';
  for (var i=0; i &lt; arguments.length; i++) {
    if (arguments[i].length &gt; longest.length) {
      longest = arguments[i];
    }
  }
  return longest;
}
</pre>

<p>You can use <code><a href="/en-US/docs/JavaScript/Reference/Functions_and_function_scope/arguments/length">arguments.length</a></code> to count how many arguments the function was called with. If you instead want to count how many parameters a function is declared to accept, inspect that function's <code><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Function/length">length</a></code> property.</p>

<h3 id="Utilizar_typeof_com_Arguments">Utilizar <code>typeof</code> com <em>Arguments</em></h3>

<p>The <code><a href="/en-US/docs/Web/JavaScript/Reference/Operators/typeof">typeof</a></code> operator returns <code>'object'</code> when used with <code>arguments</code></p>

<pre class="brush: js">console.log(typeof arguments); // 'object' </pre>

<p>The type of individual arguments can be determined by indexing <code>arguments</code>:</p>

<pre>console.log(typeof arguments[0]); // returns the type of the first argument</pre>

<h2 id="Propriedades">Propriedades</h2>

<dl>
 <dt></dt>
 <dt>{{jsxref("Functions/arguments/callee", "arguments.callee")}}</dt>
 <dd>Reference to the currently executing function that the arguments belong to.</dd>
 <dt>{{jsxref("Functions/arguments/length", "arguments.length")}}</dt>
 <dd>The number of arguments that were passed to the function.</dd>
 <dt>{{jsxref("Functions/arguments/@@iterator", "arguments[@@iterator]")}}</dt>
 <dd>Returns a new {{jsxref("Array/@@iterator", "Array iterator", "", 0)}} object that contains the values for each index in <code>arguments</code>.</dd>
</dl>

<h2 id="Exemplos">Exemplos</h2>

<h3 id="Definir_uma_function_que_concatena_várias_strings">Definir uma <em>function</em> que concatena várias <em>strings</em></h3>

<p>This example defines a function that concatenates several strings. The function's only formal argument is a string containing the characters that separate the items to concatenate.</p>

<pre class="brush:js">function myConcat(separator) {
  var args = Array.prototype.slice.call(arguments, 1);
  return args.join(separator);
}</pre>

<p>You can pass as many arguments as you like to this function. It returns a string list using each argument in the list:</p>

<pre class="brush:js">// returns "red, orange, blue"
myConcat(', ', 'red', 'orange', 'blue');

// returns "elephant; giraffe; lion; cheetah"
myConcat('; ', 'elephant', 'giraffe', 'lion', 'cheetah');

// returns "sage. basil. oregano. pepper. parsley"
myConcat('. ', 'sage', 'basil', 'oregano', 'pepper', 'parsley');</pre>

<h3 id="Definir_uma_function_que_cria_listas_HTML">Definir uma <em>function</em> que cria listas HTML</h3>

<p>This example defines a function that creates a string containing HTML for a list. The only formal argument for the function is a string that is "<code>u</code>" if the list is to be unordered (bulleted), or "<code>o</code>" if the list is to be ordered (numbered). The function is defined as follows:</p>

<pre class="brush:js">function list(type) {
  var html = '&lt;' + type + 'l&gt;&lt;li&gt;';
  var args = Array.prototype.slice.call(arguments, 1);
  html += args.join('&lt;/li&gt;&lt;li&gt;');
  html += '&lt;/li&gt;&lt;/' + type + 'l&gt;'; // end list

  return html;
}</pre>

<p>You can pass any number of arguments to this function, and it adds each argument as a list item to a list of the type indicated. For example:</p>

<pre class="brush:js">var listHTML = list('u', 'One', 'Two', 'Three');

/* listHTML is:
"&lt;ul&gt;&lt;li&gt;One&lt;/li&gt;&lt;li&gt;Two&lt;/li&gt;&lt;li&gt;Three&lt;/li&gt;&lt;/ul&gt;"
*/</pre>

<h3 id="Parâmetros_Rest_predefinição_e_desestruturados">Parâmetros Rest, predefinição, e desestruturados</h3>

<p>The <code>arguments</code> object can be used in conjunction with <a href="/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters">rest</a>, <a href="/en-US/docs/Web/JavaScript/Reference/Functions/Default_parameters">default</a>, and <a href="/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment">destructured</a> parameters.</p>

<pre class="brush: js">function foo(...args) {
  return args;
}
foo(1, 2, 3); // [1,2,3]
</pre>

<p>While the presence of rest, default, or destructured parameters does not alter the <a href="/en-US/docs/Web/JavaScript/Reference/Strict_mode#Making_eval_and_arguments_simpler">behavior of the <code>arguments</code> object in strict mode code</a>, there is a subtle difference for non-strict code.</p>

<p>In strict-mode code, the <code>arguments</code> object behaves the same whether or not a function is passed rest, default, or destructured parameters. That is, assigning new values to variables in the body of the function will not affect the <code>arguments</code> object. Nor will assigning new variables to the <code>arguments</code> object affect the value of variables.</p>

<div class="blockIndicator note">
<p><strong>Nota:</strong> You cannot write a <code>"use strict";</code> directive in the body of a function definition that accepts rest, default, or destructured parameters. Doing so will throw <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Strict_Non_Simple_Params">a syntax error</a>.</p>
</div>

<p>Non-strict functions that are passed only simple parameters (that is, not rest, default, or restructured parameters) will sync the value of variables new values in the body of the function with the <code>arguments</code> object, and vice versa:</p>

<pre class="brush: js">function func(a) {
  arguments[0] = 99; // updating arguments[0] also updates a
  console.log(a);
}
func(10); // 99
</pre>

<p>And also:</p>

<pre class="brush: js">function func(a) {
  a = 99; // updating a also updates arguments[0]
  console.log(arguments[0]);
}
func(10); // 99
</pre>

<p>Conversely, non-strict functions that <strong>are</strong> passed rest, default, or destructured parameters <strong>will not</strong> sync new values assigned to argument variables in the function body with the <code>arguments</code> object. Instead, the <code>arguments</code> object in non-strict functions with complex parameters <strong>will always</strong> reflect the values passed to the function when the function was called (this is the same behavior as exhibited by all strict-mode functions, regardless of the type of variables they are passed):</p>

<pre class="brush: js">function func(a = 55) {
  arguments[0] = 99; // updating arguments[0] does not also update a
  console.log(a);
}
func(10); // 10</pre>

<p>E também:</p>

<pre class="brush: js">function func(a = 55) {
  a = 99; // updating a does not also update arguments[0]
  console.log(arguments[0]);
}
func(10); // 10
</pre>

<p>E também:</p>

<pre class="brush: js">// An untracked default parameter
function func(a = 55) {
  console.log(arguments[0]);
}
func(); // undefined</pre>

<h2 id="Especificações">Especificações</h2>

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">Especificação</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-arguments-exotic-objects', 'Arguments Exotic Objects')}}</td>
  </tr>
 </tbody>
</table>

<h2 id="Compatibildiade_de_navegador">Compatibildiade de navegador</h2>



<p>{{Compat("javascript.functions.arguments")}}</p>

<h2 id="Consulte_também">Consulte também</h2>

<ul>
 <li>{{JSxRef("Function")}}</li>
 <li><a href="/pt-PT/docs/Web/JavaScript/Reference/Funcoes/parametros_rest">Parâmetros Rest</a></li>
</ul>