Javascript learning notes function (4): arguments object, learning notes arguments

Source: Internet
Author: User

Javascript learning notes function (4): arguments object, learning notes arguments

Each Javascript function can access a special variable-arguments in its own scope. This variable contains a list of all parameters 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 attribute. But it does not inherit from Array. prototype. In fact, it is an object.
Therefore, we cannot directly use some array methods for arguments, such as push, pop, or slice. To use these methods, we need to convert them into a real array.

Convert to array

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

Array. prototype. slice. call (arguments );
Because the conversion speed is very slow, we do not recommend this in programs with strict performance requirements.

PASS Parameters

The following is a recommended method to pass an arguments object from one function to another.

Copy codeThe Code is as follows:
Function foo (){
Bar. apply (null, arguments );
}
Function bar (a, B, c ){
// Do stuff here
}

Another clever method is to use call and apply to quickly create an unassociated outer method.

Copy codeThe Code is as follows:
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 );
};

Relationship between function parameters and arguments attributes

The arguments object creates the getter and setter methods for its own attributes and function parameters.
Therefore, modifying the function parameters affects the attribute values of the corresponding arguments object, and vice versa.

Copy codeThe Code is 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 problems

Arguments is not created only in two cases. One is declared as a local variable in the function, and the other is used as a form parameter of the function. In other cases, the arguments object is always created.
Since the getter and setter methods are always created with the creation of the arguments object, the use of arguments has almost no impact on the performance.
However, there is a situation that seriously affects Javascript performance, that is, using arguments. callee.

Copy codeThe Code is 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 (); // wocould normally be inlined...
}
}

In the above 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 improvement brought about by Inline expansion, but also damages the function encapsulation, because the function itself may depend on a specific call background.
Therefore, we recommend that you do not use arguments. callee.

The above is all about the Javascript arguments object. Are you familiar with it?

Arguments refers to the parameter object of the function (refers to the actual input parameter)
Arguments. length indicates the length of the parameter object of the function.
Arguments [I] indicates the value of parameter I (the first is 0)

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.