JS function overload solution

  • 2020-03-30 02:58:21
  • OfStack

One, for the different number of parameters of the overload
It should be clear here, just use the length property of the arguments function.



<script type="text/javascript">
function talk(msg,handler){ 
     var len = arguments.length; 
    //When an argument is passed in
    if(len==1){ 
    alert("Function say:"+msg); 
    } 
    //When two arguments are passed
    else if(len==2){ 
         handler(msg); 
     } 
} 
talk("demo"); 
talk("demo",function(w){alert("Handler say:"+w);}); 
</script> 

Overloading for different types of parameters  
For JS such a dynamically typed language, the variable declaration of arbitrary played down the strict variable types in mind the importance of developers (PS: is based on the ECMA system, same AS introduced variable declarations of forced type), a lot of unexpected bugs are caused by the automatic conversion of the variable type. In fact, JS provides a very accurate way for us to strictly detect the typeof a variable. The common ones are the typeof method and the constructor attribute.

Typeof variable returns the typeof a variable


temp = "say"; //string 
temp = 1; //number 
temp = undefined; //undefined 
temp = null; //object 
temp = {}; //object 
temp = []; //object 
temp = true; //boolean 
temp = function (){} //function 
alert(typeof temp); 

      As you can see from the above test, null,Object, and Array return Object types, and the following method can solve this problem.

2. The constructor attribute detects the variable type

      Each object in JS has the constructor attribute, which is a function that constructs the object by reference, and this reference is used to detect the variable type.


temp = "say"; 
temp.constructor==String; //true 
temp= {}; 
temp.constructor == Object;//true 
temp= []; 
temp.constructor == Array;//true 

      The above test makes it easy to distinguish the Array from the Object type variables. Now let's test our custom object and see what happens.


//Custom object
function Ball(){} 
//Instantiate an object
var basketBall = new Ball(); 
basketBall.constructor==Ball; //true 

      This demonstrates that the constructor attribute applies to custom objects as well.

With the above two methods in mind, I'll return to the simulation of JS function overloading. The following example is an example of overloading by parameter type.


function talk(msg){ 
     var t = typeof msg; 
     if(t=="string"){ 
            alert("It's a string"); 
    } 
    else if(t=="number"){ 
            alert("It's a number"); 
     } 
} 
talk(10); //It's a string 
talk("demo"); //It's a number 

Attached is a very clever function that strictly detects the type and number of parameters:


//Strictly checks the type of a list of variables against the parameter list
function strict( types, args ) { 
     //Ensure that the number of parameters matches the type kernel
     if ( types.length != args.length ) { 
            //If the length does not match, an exception is thrown
           throw "Invalid number of arguments. Expected " + types.length + ", received " + args.length + " instead."; 
    } 
    //Iterate through each parameter, checking the base type
    for ( var i = 0; i < args.length; i++ ) { 
          //If a JavaScript item type does not match, an exception is thrown
          if ( args[i].constructor != types[i] ) { 
                throw "Invalid argument type. Expected " + types[i].name +", received " + args[i].constructor.name + " instead."; 
          } 
     } 
} 
//The use of the above methods
function doFunction(id,name){ 
     //Detects the number and type of parameters
     strict([Number,String],arguments); 
  .. 
}


Related articles: