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.