Arguments object verification function parameters are valid _ javascript skills

Source: Internet
Author: User
The parameter object arguments in Javascript functions is an object, not an array. However, it can access the elements in the array through the following numeric table, and it also has the length attribute to identify the number of its elements. Use the arguments object to verify whether the function parameters are valid.

Script function sum (arg1, arg2) // addition function {var realArgCount = arguments. length; // The number of arguments passed when the function is called var frmArgCount = sum. length; // number of parameters in function definition if (realArgCount <frmArgCount) // if the actual number of parameters is smaller than the number of parameters {var e = new Error (); // defines the Error message, and then throw e. number = 1000001; // error code e. message = "The actual number of parameters does not meet the requirements! "// Error message throw e;} return arguments [0] + arguments [1]; // if the parameters meet the requirements, extract the real parameters from the arguments object and return the sum of the two.} try {document. write ("

Arguments object test

"); // Output the title document. write (" correct call result: "+ sum (); // output the correct call result document. write ("
Result of a call that does not comply with the rules: "); // manually triggers a call method document that does not comply with the rules. write (sum (10);} catch (e) // catch error message {alert (e. number + "error code:" + e. message);} script

The above is all the content of this article. I hope you will like it.

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.