JavaScript Learning Notes function chapter (iv): arguments Object _ Basics

Source: Internet
Author: User

Each Javascript function can access a special variable-arguments within its scope. This variable contains a list of all the arguments passed to the function.
The arguments object is not an array. Although syntactically it has the same place as an array, for example, it has the length property. But it is not inherited from Array.prototype, in fact, it is an object.
Therefore, we cannot use some array methods directly against arguments, such as push, pop, or slice. So in order to use these methods, we need to convert them to a real array.

Converting to arrays

The following code returns an array containing all the elements of the arguments object.

Array.prototype.slice.call (arguments);
Because of the slow speed of conversion, this is not recommended in highly performance-demanding programs.

Passing parameters

The following is a more recommended method of passing a arguments object from one function to another.

Copy Code code as follows:

function foo () {
Bar.apply (null, arguments);
}
function Bar (A, B, c) {
Do stuff here
}

Another ingenious approach is to use call and apply to quickly create a unbound outer method.

Copy Code code as follows:

function Foo () {}
Foo.prototype.method = function (A, B, c) {
Console.log (This, a, b, c);
};
Create a 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);
};

Relationship between function parameters and arguments properties

The arguments object creates getter and setter methods for the parameters of its own properties and functions.
Therefore, modifying the function's arguments affects the property values of the corresponding object, and vice versa.

Copy Code code as follows:

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 is not created in two cases, one is declared as a local variable within the function, and the other is the formal parameter of the function. In other cases, the arguments object is always created.
Because getter and setter methods are always created with the creation of arguments objects, using arguments has little impact on performance itself.
However, one scenario can seriously affect the performance of Javascript, which is using Arguments.callee.

Copy Code code as follows:

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 preceding code, the Foo function is no longer a simple inline extension, because it needs to know itself and its caller (caller). This not only offsets the performance boost from inline extensions, but also undermines the encapsulation of functions because the function itself may need to rely on a particular invocation background.
Therefore, we suggest that we try not to use Arguments.callee.

This is all about JavaScript arguments objects, whether the small partners understand thoroughly, simply say

Arguments refers to the parameter object of the function (that is, the actual arguments passed in)
Arguments.length refers to the length of the parameter object of the function
Arguments[i] refers to the value of the parameter I (the first one is 0)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.