201506170744_ JavaScript authoritative Guide (sixth edition)-function call variable length arguments and optional parameters, "(p171-175)

Source: Internet
Author: User

I. Constructor call (with or without return)

1. var obj = new Object ();

var obj = new Object;

The two lines of code are equivalent; expression: New obj.m ();, this is not obj!!

The constructor is usually not used with the return keyword;

Two. Indirect calls

1. Call (), and apply (); Function: Any object can call any method/

Call ();--use its own arguments as arguments to the function;

Apply ();--to pass in an argument as an array;

2. Optional parameter: var a = a | | [];

3. Variable-length arguments: Arguments is a reference to an argument;

For example:

function Add (a,b,c) {

if (arguments.length! = 3) {

throw new Error ("The number of arguments actually passed in is:" + Arguments.length + ", but the function defines the number of formal parameters is 3!) ");

}else{

Return a + B + C

}

};

Add (1,1,2,9); Error: The actual number of arguments passed in is: 4, but the function defines the number of formal parameters is 3!

3. Function Add (a,b,c) {if (arguments.length! = 3) {throw new Error ("The number of arguments actually passed in is:" + Arguments.length + ", but the function defines the number of formal parameters is 3!) ");} Else{arguments[0] = Null;return A + B + c}};  Add (9,.); The result is: 3. "9" is set to "null" value;

4. Arguments in strict mode it cannot be assigned, and cannot be used as a reserved word for formal parameter names.

5.

201506170744_ JavaScript authoritative Guide (sixth edition)-function call variable length arguments and optional parameters, "(p171-175)

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.