JavaScript arguments object application example, javascript example

Source: Internet
Author: User

JavaScript arguments object application example, javascript example

Arguments object

In function code, the special object arguments can be accessed without explicitly specifying the parameter names.

For example, in the sayHi () function, the first parameter is message. You can also access this value using arguments [0], that is, the value of the first parameter (the first parameter is at the position 0, and the second parameter is at the position 1, and so on ).

Therefore, you can rewrite the function without specifying the parameter:

function sayHi() {if (arguments[0] == "bye") {return;}alert(arguments[0]);}

Detection parameter count

You can also use the arguments object to check the number of parameters of the function and reference the arguments. length attribute.

The following code outputs the number of parameters used for each function call:

function howManyArgs() {alert(arguments.length);}howManyArgs("string", 45);howManyArgs();howManyArgs(12);

The above code displays "2", "0", and "1" in sequence ".

Note: unlike other programming languages, ECMAScript does not verify that the number of parameters passed to a function is equal to the number of parameters defined by the function. All functions defined by the developer can accept any number of parameters (according to Netscape documentation, a maximum of 255 parameters can be accepted) without causing any errors. Any missing parameters will be passed to the function as undefined, and additional functions will be ignored.
Simulate function Overloading

You can use the arguments object to determine the number of parameters passed to the function to simulate function overloading:

function doAdd() {if(arguments.length == 1) {alert(arguments[0] + 5);} else if(arguments.length == 2) {alert(arguments[0] + arguments[1]);}}

DoAdd (10); // output "15"
DoAdd (40, 20); // output "60"

When there is only one parameter, the doAdd () function adds 5 to the parameter. If two parameters exist, the two parameters are added and their sum is returned. Therefore, doAdd (10) Outputs "15", while doAdd (40, 20) Outputs "60 ".

It is not as good as heavy load, but it is enough to avoid the restriction of ECMAScript.


Problems with arguments in Javascript Functions

Arguments is a pseudo array. The for in traversal object may be different in different browsers. Therefore, it is better to use the traditional

Javascript Problems

Arguments attributes
Returns an arguments object for the currently executed function object.

Function. arguments

The function parameter is the name of the currently executed function, which can be omitted.

Description
The arguments attribute allows the function to process variable numbers of parameters. The length attribute of the arguments object contains the number of parameters passed to the function. For a single parameter contained in an arguments object, the access method is the same as the access method of the parameter contained in the array.

Example
The following example illustrates the usage of the arguments attribute:

Function ArgTest (){
Var I, s, numargs = arguments. length;
S = numargs;
If (numargs <2)
S + = "argument was passed to ArgTest. It was ";
Else
S + = "arguments were passed to ArgTest. They were ";
For (I = 0; I <numargs; I ++)
{
S + = arguments [I] + "";
}
Return (s );
}

Arguments object
This object represents the function being executed and the parameters of the function that calls it.

[Function.] arguments [n]

Parameters
Function

Optional. Name of the Function object currently being executed.

N

Required. The index of the parameter value starting from 0 to be passed to the Function object.

Description
You cannot create an arguments object explicitly. The arguments object is available only when the function starts. The arguments object of the function is not an array. Accessing a single parameter is the same as accessing an array element. Index n is actually the 0 of the arguments object... One of the n attributes.

Example
The following example demonstrates the usage of the arguments object.

Function ArgTest (a, B ){
Var I, s = "The ArgTest function expected ";
Var numargs = arguments. length; // obtain the value of the passed parameter.
Var expargs = ArgTest. length; // obtain the value of the expected parameter.
If (expargs <2)
S + = expargs + "argument .";
Else
S + = expargs + "arguments .";
If (numargs <2)
S + = numargs + "was passed .";
Else
S + = numargs + "were passed .";
S + = "\ n"
For (I = 0; I <numargs; I ++) {// obtain the parameter content ....... Remaining full text>

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.