Arguments objects
A special variable arguments can be accessed within each function in JavaScript. This variable maintains all the list of arguments passed into this function.
Note: Since arguments has been defined as a variable within a function.
So by defining arguments with the var keyword or by declaring arguments as a form parameter,
Will cause the native arguments to not be created.
The arguments variable is not an array.
Although it has an array-related property length in syntax, it does not inherit from Array.prototype, which is actually an object.
Therefore, you cannot use standard array methods for arguments variables, such as push, pop, or slice.
Although it is possible to use a For loop traversal, it is best to convert it to a true array for better use of the array method.
Converting to arrays
The following code creates a new array that contains the elements in all arguments objects.
Array.prototype.slice.call (arguments);
This conversion is slow and is not recommended in poorly performing code.
Passing parameters
The following is the recommended procedure for passing parameters from one function to another.
function foo () {
Bar.apply (null, arguments);
}
function Bar (A, B, c) {
Do stuff here
}
Another trick is to create a fast binding wrapper using call and apply.
function Foo () {}
Foo.prototype.method = function (A, B, c) {
Console.log (This, a, b, c);
};
Create a unbound version of "method"
The input parameters are: This, arg1, Arg2...argn
Foo.method = function () {
Results: Foo.prototype.method.call (this, arg1, arg2 ... argn)
Function.call.apply (Foo.prototype.method, arguments);
};
Translator Note: The above Foo.method function and the following code effect is the same:
Foo.method = function () {
var args = Array.prototype.slice.call (arguments);
Foo.prototype.method.apply (Args[0], Args.slice (1));
};
Automatic Updates
The arguments object creates getter and setter methods for its internal properties and function form parameters.
Therefore, changing the value of the formal parameter affects the value of the arguments object, and vice versa.
function foo (A, B, c) {
Arguments[0] = 2;
A 2
b = 4;
ARGUMENTS[1]; 4
var d = c;
D = 9;
C 3
}
Foo (1, 2, 3);
Performance Truth
Arguments objects are always created, except for two special cases-declarations as local variables and as formal arguments.
Regardless of whether it has been used or not.
Arguments's getters and setters methods will always be better, so using arguments has little effect on performance.
Unless you need to make multiple accesses to the properties of the arguments object.
ES5 hint: These getters and setters are not created in strict mode (strict mode).