Usage of arguments keywords in javascript

Source: Internet
Author: User

since the JS call function can not be in accordance with the function of the definition format to pass parameters, for example:

function Test (A, b) {do   something}
Test (1);
Test (from);
Test ();

The above three call methods are correct, but if the test function uses a A, b parameter, the test () Call function may return a undefined error . So JS provides a keyword arguments to get the parameters passed in. Arguments is an array with the Length property, which can be obtained by arguments[index] to get the parameter values passed in by the function.

For example:

function addition (A, b) {   return a+b;//if a, B is not passed in will be an error! }//Improved Method Funciton Additionbetter (A, b) {   if (arguments.length==0)     return 0;   else if (arguments.length==1) {     return arguments[0];   } else{     return arguments[0]+arguments[1];}   }


Usage of arguments keywords in javascript

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.