JS simulation implementation method overload example

  • 2021-07-07 06:24:55
  • OfStack

In this paper, an example is given to describe the overload of JS simulation implementation method. Share it for your reference, as follows:

In the JS method, overloading cannot be implemented like the C # method, but we can implement overloading of the js method through arguments in js.

An example of html is given below:


<html>
<head>
<title>JS Overload of 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 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 called method 4
  /// </summary>
  /// <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 Heavy load test method 
  /// </summary>
  /// <param name="arm1"></param>
  /// <param name="arm2"></param>
  /// <param name="arm3"></param>
  /// <param name="arm4"></param>
  if(arguments.length==1){
     alert(' I am JS The parameters of the overloaded method of the 1 Shared '+arguments.length+' A. \n Parameter 1 : '+arguments[0]);
     // This is both an overloaded method 
     testFun1(arm1);
  }
  else if(arguments.length==2){
     alert(' I am JS The parameters of the overloaded method of the 1 Shared '+arguments.length+' A. \n Parameter 1 : '+arguments[0]+'\n Parameter 2 : '+arguments[1]);
     // This is both an overloaded method 
     testFun2(arm1,arm2);
  }
  else if(arguments.length==3){
     alert(' I am JS The parameters of the overloaded method of the 1 Shared '+arguments.length+' A. \n Parameter 1 : '+arguments[0]+'\n Parameter 2 : '+arguments[1]+'\n Parameter 3 : '+arguments[2]);
     // This is both an overloaded method 
     testFun3(arm1,arm2,arm3);
  }
  else if(arguments.length==4){
     alert(' I am JS The parameters of the overloaded method of the 1 Shared '+arguments.length+' A. \n Parameter 1 : '+arguments[0]+'\n Parameter 2 : '+arguments[1]+'\n Parameter 3 : '+arguments[2]+'\n Parameter 4 : '+arguments[3]);
     // This is both an overloaded method 
     testFun4(arm1,arm2,arm3,arm4);
  }
  // Multiple parameters and so on 
}
// Initialize the executed method 
window.onload=function(){
  testFun(1,2,3);
}
</script>
</head>
<body>
  JS Overload of method 
</body>
</html>

More readers interested in JavaScript can check the topics of this site: "javascript Object-Oriented Introduction Tutorial", "JavaScript Data Structure and Algorithm Skills Summary", "JavaScript Mathematical Operation Usage Summary", "JavaScript Switching Special Effects and Skills Summary", "JavaScript Finding Algorithm Skills Summary", "JavaScript Animation Special Effects and Skills Summary", "JavaScript Error and Debugging Skills Summary" and "JavaScript Traversal Algorithm and Skills Summary"

I hope this article is helpful to everyone's JavaScript programming.


Related articles: