داوطلبان ما هنوز مقاله را به فارسی ترجمه نکرده‌اند. عضو بشوید و به ما کمک کنید!
شما همچنین می‌توانید مقاله را به زبان English (US) بخوانید.

The arguments object is an Array-like object corresponding to the arguments passed to a function.

Syntax

arguments

Description

The arguments object is a local variable available within all (non-arrow) functions. You can refer to a function's arguments within the function by using the arguments object. This object contains an entry for each argument passed to the function, the first entry's index starting at 0. For example, if a function is passed three arguments, you can refer to them as follows:

arguments[0]
arguments[1]
arguments[2]

The arguments can also be set:

arguments[1] = 'new value';

The arguments object is not an Array. It is similar to an Array, but does not have any Array properties except length. For example, it does not have the pop method. However it can be converted to a real Array:

var args = Array.prototype.slice.call(arguments);
var args = [].slice.call(arguments);

// ES2015
const args = Array.from(arguments);

Using slice on arguments prevents optimizations in some JavaScript engines (V8 for example - more information). If you care for them, try constructing a new array by iterating through the arguments object instead. An alternative would be to use the despised Array constructor as a function:

var args = (arguments.length === 1 ? [arguments[0]] : Array.apply(null, arguments));

You can use the arguments object if you call a function with more arguments than it is formally declared to accept. This technique is useful for functions that can be passed a variable number of arguments. Use arguments.length to determine the number of arguments passed to the function, and then process each argument by using the arguments object. To determine the number of parameters in the function signature, use the Function.length property.

Using typeof with Arguments

The typeof arguments returns 'object'. 

console.log(typeof arguments); // 'object' 

The typeof individual arguments can be determined with the use of indexing.

console.log(typeof arguments[0]); //this will return the typeof individual arguments.

Using the Spread Syntax with Arguments

You can also use the Array.from() method or the spread operator to convert arguments to a real Array:

var args = Array.from(arguments);
var args = [...arguments];

Properties

arguments.callee
Reference to the currently executing function.
arguments.caller
Reference to the function that invoked the currently executing function.
arguments.length
Reference to the number of arguments passed to the function.
arguments[@@iterator]
Returns a new Array Iterator object that contains the values for each index in the arguments.

Examples

Defining a function that concatenates several strings

This example defines a function that concatenates several strings. The only formal argument for the function is a string that specifies the characters that separate the items to concatenate. The function is defined as follows:

function myConcat(separator) {
  var args = Array.prototype.slice.call(arguments, 1);
  return args.join(separator);
}

You can pass any number of arguments to this function, and it creates a list using each argument as an item in the list.

// 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');

Defining a function that creates HTML lists

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 "u" if the list is to be unordered (bulleted), or "o" if the list is to be ordered (numbered). The function is defined as follows:

function list(type) {
  var result = '<' + type + 'l><li>';
  var args = Array.prototype.slice.call(arguments, 1);
  result += args.join('</li><li>');
  result += '</li></' + type + 'l>'; // end list

  return result;
}

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

var listHTML = list('u', 'One', 'Two', 'Three');

/* listHTML is:

"<ul><li>One</li><li>Two</li><li>Three</li></ul>"

*/

Rest, default, and destructured parameters

The arguments object can be used in conjunction with rest, default, and destructured parameters.

function foo(...args) {
  return args;
}
foo(1, 2, 3); // [1,2,3]

While the presence of restdefault, or destructured parameters does not alter the behavior of the arguments object in strict mode code, there is a subtle difference for non-strict code.

When a non-strict function does not contain restdefault, or destructured parameters, then the values in the arguments object do track the values of the arguments (and vice versa). See the code below:

function func(a) { 
  arguments[0] = 99; // updating arguments[0] also updates a
  console.log(a);
}
func(10); // 99

and

function func(a) { 
  a = 99; // updating a also updates arguments[0]
  console.log(arguments[0]);
}
func(10); // 99

When a non-strict function does contain restdefault, or destructured parameters, then the values in the arguments object do not track the values of the arguments (and vice versa). Instead, they reflect the arguments provided at the time of invocation:

function func(a = 55) { 
  arguments[0] = 99; // updating arguments[0] does not also update a
  console.log(a);
}
func(10); // 10

and

function func(a = 55) { 
  a = 99; // updating a does not also update arguments[0]
  console.log(arguments[0]);
}
func(10); // 10

and

function func(a = 55) { 
  console.log(arguments[0]);
}
func(); // undefined

Specifications

Specification Status Comment
ECMAScript 1st Edition (ECMA-262) Standard Initial definition. Implemented in JavaScript 1.1
ECMAScript 5.1 (ECMA-262)
The definition of 'Arguments Object' in that specification.
Standard  
ECMAScript 2015 (6th Edition, ECMA-262)
The definition of 'Arguments Exotic Objects' in that specification.
Standard  
ECMAScript Latest Draft (ECMA-262)
The definition of 'Arguments Exotic Objects' in that specification.
Living Standard  

Browser compatibility

FeatureChromeEdgeFirefoxInternet ExplorerOperaSafari
Basic support Yes Yes Yes Yes Yes Yes
callee Yes Yes Yes6 Yes Yes
caller No No No Yes — 9 No No
length Yes Yes Yes Yes Yes Yes
@@iterator52 ?46 No Yes9
FeatureAndroid webviewChrome for AndroidEdge mobileFirefox for AndroidIE mobileOpera AndroidiOS Safari
Basic support Yes Yes Yes Yes Yes Yes Yes
callee Yes Yes Yes Yes8.1 Yes Yes
caller No No No No No No No
length Yes Yes Yes Yes Yes Yes Yes
@@iterator5252 ?46 No Yes9

See also