Javascript does not have a function signature like other programming languages (what is a function signature? Simply put, it refers to the function’s accepted parameter type and number of parameters. Some people think that the return type should also be included. You can check the specific concepts online. ).
So Javascript cannot implement overloading with the same method name and different number of parameters like other languages. If you don’t believe me, you can try:
Under breakpoint debugging, the show method without parameters will not be executed, and will be overwritten by the show(num1) method.
So "overloading" cannot be implemented in Javascript? The answer is yes, it's just another way. Yes, just use arguments.
So what are arguments? It is a special attribute in JS. It can get the value of the parameter through the subscript index like an array (but it is not an array), and get the number of parameters through length:
Another thing to know is that named parameters of functions in JS are not necessary, so if you want to know how many parameters are passed when calling, you still have to get the parameters through arguments.
The following is a simple method overloading:
The code is as follows: