Common Methods for parameter detection during function calling in Javascript

Source: Internet
Author: User

1. Method overload

JS does not directly support method overloading similar to C #, so it can only be solved in disguise.Code(Using the built-in attribute arguments)

 
VaR F1 = function (P1, P2, P3) {Switch (arguments. length) {Case 0: Alert ("F1 without Parameters") break; Case 1: Alert ("F1 with 1 parameter version:" + p1) break; Case 2: alert ("Two Parameter versions of F1:" + p1 + "," + p2) break; Case 3: Alert ("three parameter versions of F1: "+ p1 +", "+ p2 +", "+ P3) break; default: Alert (" more than three parameters cannot be called! "); Break;} F1 (); F1 (" 1 "); F1 (" A ", 100); F1 (" 1 "," 2 ", "3"); F1 ("1", "2", "3", "4 ")

2. Check the number of parameters

The JS engine does not forcibly check the number of parameters when calling the function, so it can only be processed by itself. Example code:

VaR fnmustoneparam = function (p) {// check whether there are any parameters passed in if (typeof P = "undefined") {alert ("fnmustoneparam must have parameters passed in, can be called (1 )! "); Return;} // You can also write it as if (arguments. Length = 0) {alert (" fnmustoneparam must have a parameter passed in before calling (2 )! "); Return;} // check the number of parameters if (arguments. length! = 0) {alert ("fnmustoneparam can be passed in only one parameter call! "); Return;} // to do...} // fnmustoneparam (1, 3, 4 );

3. Basic Parameter type detection
The JS engine does not detect the parameter type. If you want to limit the basic type of the parameter, you can use typeof to determine the basic type.

 
VaR fnstring = function (s) {If (arguments. length! = 1) {alert ("the number of parameters does not match! "); Return;} If (typeof s! = "String") {alert ("can only be a string type parameter! "); Return ;}// fnstring (123 );

4. Custom class parameter type detection
The method mentioned in Article 3rd can only detect the basic types of parameters. If a parameter of a custom class is used and the typeof operator number is used, only the object type detection result can be obtained, you can use the instanceof operator number to solve this problem.

 function person (name, age) {This. name = Name; this. age = age;} function fnperson (p) {If (arguments. length = 1 & P instanceof person) {// If (arguments. length = 1 & P. constructor = person) // You can also write alert ("fnperson called successfully, P. name = "+ P. name + ", p. age = "+ P. age);} else {alert ("You must input a person-type parameter before calling! ") ;}} Fnperson (" ASDF "); fnperson (new person ('yang Ta' under the bodhi tree, 30) 

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.