This article describes the overloading of JS simulation implementation method. Share it for your reference, as follows:
In JS methods, overloading cannot be achieved like C# methods, but we can implement overloading of js methods through arguments in js.
Here is an example of html:
<html><head><title>Overload of JS method</title><script>function testFun1(arm1){ /// <summary> /// JS overload test called method 1 /// </summary> /// <param name="arm1"></param> alert(arm1);}function testFun2(arm1,arm2){ /// <summary> /// JS overload test called method 2 /// </summary> /// <param name="arm1"></param> /// <param name="arm2"></param> alert(arm1+','+arm2);}function testFun3(arm1,arm2,arm3){ /// <summary> /// JS overload test is called method 3 /// </summary> /// <param name="arm1"></param> /// <param name="arm2"></param> /// <param name="arm3"></param> alert(arm1+','+arm2+','+arm3);}function testFun4(arm1,arm2,arm3,arm4){ /// <summary> /// JS overload test is called method 4 /// </summary> /// <param name="arm1"></param> /// <param name="arm1"></param> /// <param name="arm1"></param> /// <param name="arm1"></param> /// <param name="arm2"></param> /// <param name="arm3"></param> /// <param name="arm4"></param> alert(arm1+','+arm2+','+arm3+','+arm4);}function testFun(arm1,arm2,arm3,arm4){ /// <summary> /// JS overload test method/// </summary> /// <param name="arm1"></param> /// <param name="arm2"></param> /// <param name="arm3"></param> /// <param name="arm3"></param> /// <param name="arm4"></param> if(arguments.length==1){ alert('I am an overloaded method of JS, and the parameters of this method are '+arguments.length+'. /n Parameter 1: '+arguments[0]); //This is the method that executes overload testFun1(arm1); } else if(arguments.length==2){ alert('I am an overloaded method of JS, and the parameters of this method are '+arguments.length+'. /n Parameter 1: '+arguments[0]+'/n Parameter 2: '+arguments[1]); //This is the method that executes overload testFun2(arm1,arm2); } else if(arguments.length==3){ alert('I am an overloaded method of JS, and the parameters of this method are '+arguments.length+'. /n Parameter 1: '+arguments[0]+'/n Parameter 2: '+arguments[1]+'/n Parameter 3: '+arguments[2]); //This is the method that executes overload testFun3(arm1,arm2,arm3); } else if(arguments.length==4){ alert('I am an overloaded method of JS, and the parameters of this method are '+arguments.length+'. /n Parameter 1: '+arguments[0]+'/n Parameter 2: '+arguments[1]+'/n Parameter 3: '+arguments[2]+'/n Parameter 4: '+arguments[3]); //This is the method to execute overload testFun4(arm1,arm2,arm3,arm4); } //Multiple parameters and so on}//Initialize the method to execute window.onload=function(){ testFun(1,2,3);}</script></head><body> Overloading of JS method</body></html>For more information about JavaScript related content, please check out the topics of this site: "Javascript object-oriented tutorial", "Summary of JavaScript data structures and algorithm techniques", "Summary of JavaScript mathematical operations usage", "Summary of JavaScript switching effects and techniques", "Summary of JavaScript search algorithm techniques", "Summary of JavaScript animation effects and techniques", "Summary of JavaScript errors and debugging techniques" and "Summary of JavaScript traversal algorithms and techniques"
I hope this article will be helpful to everyone's JavaScript programming.