


Javascript Study Notes - Functions (4): arguments object_basic knowledge
May 16, 2016 pm 04:30 PMEvery Javascript function can access a special variable in its own scope - arguments. This variable contains a list of all arguments passed to the function.
The arguments object is not an array. Although syntactically it has the same features as an array, for example it has a length property. But it does not inherit from Array.prototype, in fact, it is an object.
Therefore, we cannot directly use some array methods such as push, pop or slice on arguments. So in order to use these methods, we need to convert it to a real array.
Convert to array
The following code will return an array containing all elements of the arguments object.
Array.prototype.slice.call(arguments);
Since the conversion is very slow, this is not recommended in performance-critical programs.
Pass parameters
The following is a recommended way to pass the arguments object from one function to another.
function foo() {
bar.apply(null, arguments);
}
function bar(a, b, c) {
// do stuff here
}
Another clever method is to use call and apply at the same time to quickly create an unbound outer method.
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 function parameters and arguments attribute
Thearguments object creates getter and setter methods for both its own properties and the formal parameters of its functions.
Therefore, modifying the formal parameters of a function will affect the property values 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 will not be created in only two situations, one is declared as a local variable inside the function, and the other is used as a formal parameter of the function. Otherwise, the arguments object is always created.
Since getter and setter methods are always created with the arguments object, using arguments itself has little impact on performance.
However, there is one situation that seriously affects the performance of Javascript, and that 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 above code, the foo function is no longer a simple inline extension, because it needs to know both itself and its caller. This not only negates the performance gain brought by inline expansion, but also destroys the encapsulation of the function, because the function itself may need to depend on a specific calling context.
Therefore, it is recommended that you try not to use arguments.callee.
The above is all about the Javascript arguments object. Do you guys understand it thoroughly? Simply put
arguments refers to the parameter object of the function (referring to the actual parameters passed in)
arguments.length refers to the length of the parameter object of the function
arguments[i] refers to the value of the i-th parameter (the first one is 0)

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems

How to convert MySQL query result array to object?

JavaScript and WebSocket: Building an efficient real-time weather forecasting system

Simple JavaScript Tutorial: How to Get HTTP Status Code

What is the difference between arrays and objects in PHP?

How to get HTTP status code in JavaScript the easy way

What should I pay attention to when a C++ function returns an object?
