[JS object] arguments object for getting started with JS

Source: Internet
Author: User
Every day, JS sees each other. Today, let's take a look at arguments objects and attributes. The arguments object cannot be created explicitly. The arguments object is available only when the function starts. Function Arguments The object is not an array. Accessing a single parameter is the same as accessing an array element. Index N Actually Arguments Object 0...N Attribute.

The following example demonstrates Arguments The arguments object and function are inseparable. 1 Function Argtest (A, B) {
2 VaR I, S =   " The argtest function expected " ;
3 VaR Numargs = Arguments. length; // Obtain the value of the passed parameter.
4 VaR Expargs = Argtest. length; // Obtain the expected value.
5 If (Expargs <   2 )
6 S + = Expargs +   " Argument. " ;
7 Else
8 S + = Expargs +   " Arguments. " ;
9 If (Numargs <   2 )
10 S + = Numargs +   " Was passed. " ;
11 Else
12 S + = Numargs +   " Were passed. " ;
13 S + =   " \ N "
14 For (I = 0 ; I < Numargs; I ++ ) { // Obtain the parameter content.
15 S + =   " ARG "   + I +   " = "   + Arguments [I] +   " \ N " ;
16 }
17 Return (S ); // Returns the list of parameters.
18 }


The above is the arguments object, which is closely related to the function object. That is to say, each function has its own argument attribute. Next let's take a look at the argument attribute: The currently executedFunctionObject returnsArgumentsObject. The function parameter is the name of the currently executed function, which can be omitted.

Pass Arguments For attributes (relative to functions), a function can process a variable number of parameters. Arguments Object Length Attribute contains the number of parameters passed to the function. For Arguments The access method of a single parameter contained in the object is the same as that of the parameter contained in the array. See the following example: 1 Function Argtest () {
2 VaR I, S, numargs = Arguments. length;
3 S = Numargs;
4 If (Numargs <   2 )
5 S + =   " Argument was passed to argtest. It was " ;
6 Else
7 S + =   " Arguments were passed to argtest. They were " ;
8 For (I =   0 ; I < Numargs; I ++ )
9 {
10S+ =Arguments [I]+ " ";
11}
12 Return (S );
13 }

We know that each object has its own attributes, and the arguments object is no exception. First, the access to arguments is like an array object, and each element is enumerated using 0 to arguments. Length-1. Let's take a look at the callee attribute and return the code being executed. Function Object, that is, the specified Function Object Body. Callee Attribute is Arguments A member of an object, available only when the related function is being executed. Callee The initial value of the attribute is being executed. Function Object, which allows anonymous Recursive functions . 1 Function Factorial (N) {
2 If (N <=   0 )
3 Return   1 ;
4 Else
5 Return N * Arguments. callee (n -   1 )
6 }
7 Print (factorial ( 3 ));
8


Basically speaking about the arguments object here, I wonder if there is anything else? In fact, we are already familiar with many things. Apart from the callee attribute above, we use them from time to time. OK. Today's object learning is here. The object to be viewed tomorrow will be a Boolean object.

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.