Arguments object (4) :arguments object
- 2020-03-30 04:19:50
- OfStack
Each Javascript function has access to a special variable called arguments in its scope. This variable contains a list of all the arguments passed to the function.
The arguments object is not an array. Although it has the same syntax as an array, for example it has the length property. But it doesn't inherit from array.prototype; in fact, it's just an object.
Therefore, we cannot directly use array methods such as push, pop, or slice on arguments. So in order to use these methods, we need to convert them to a real array.
Convert to an array
The following code will return an array of all the elements of the arguments object.
Array. The prototype. Slice. The call (the arguments);
Due to the slow pace of transformation, this is not recommended in performance-critical programs.
Passing parameters
Here is a more recommended way to pass arguments objects from one function to another.
function foo() {
bar.apply(null, arguments);
}
function bar(a, b, c) {
// do stuff here
}
Another neat way to quickly create an unbound outer method is to use both call and apply.
function Foo() {}
Foo.prototype.method = function(a, b, c) {
console.log(this, a, b, c);
};
// Create an unbound version of "method"
// It takes the parameters: this, arg1, arg2...argN
Foo.method = function() {
// Result: Foo.prototype.method.call(this, arg1, arg2... argN)
Function.call.apply(Foo.prototype.method, arguments);
};
The relationship between a function parameter and the arguments property
The arguments object creates getter and setter methods for both its properties and the parameter of the function.
Therefore, modifying the shape of the function affects the property value of the corresponding 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 issues
Arguments are not created only if they are declared as a local variable inside a function or as a parameter of a function. Otherwise, the arguments object is always created.
Because getter and setter methods are always created with the arguments object, using arguments has little impact on performance itself.
However, one situation that can seriously affect Javascript performance is the use of arguments.callee.
function foo() {
arguments.callee; // do something with this function object
arguments.callee.caller; // and the calling function object
}
function bigLoop() {
for(var i = 0; i < 100000; i++) {
foo(); // Would normally be inlined...
}
}
In the code above, the foo function is no longer a simple inline extension, because it needs to know itself and its caller. This not only negates the performance gains from inline extensions, but also undermines the encapsulation of functions that may depend on a particular calling context.
Therefore, it is recommended that you do not use arguments.callee.
That's all you need to know about the Javascript arguments object
Arguments refers to the argument object of the function
Arguments. Length refers to the length of the argument object to the function
Arguments [I] refers to the value of the ith argument (the first is 0)