Some people who know about JS know that there is no method overload in JS, like in C #, and some are only method overwrites, when you type two or more methods with the same name in JS, regardless of the number of parameters in the method (function), the method name can only belong to the function defined at the end. That is to say, the last method overwrites the method with the same name.
Let's look at the following code:
<SCRIPT type = "text/JavaScript"> function sayhi () {alert ("Hello, I am the first sayhi method! ");} Function sayhi () {alert (" Hello, I am the second sayhi method! ");} Function sayhi () {alert (" Hello, I am the third sayhi method! ");} Sayhi (); </SCRIPT>
The result in the browser is: Hello, I am the third sayhi method!
The last method overwrites the two methods with the same name. This is a well-known method coverage issue in Js.
Can't methods be overloaded in JS?
Let's take a look at the following code:
<SCRIPT type = "text/JavaScript"> function sayhi () {If (arguments. length = 1) {alert (arguments [0] + "Hello, I am the first sayhi method! ");} Else if (arguments. length = 2) {alert (arguments [0] + "," + arguments [1] + "Hello, I am the second sayhi method! ");} Else if (arguments. length = 3) {alert (arguments [0] + "," + arguments [1] + "," + arguments [2] + "hello, I am the third sayhi method! ") ;}} Sayhi (" Tom "); sayhi (" Tom "," Lucy "); sayhi (" Tom "," Lucy "," Jame "); </SCRIPT>
When viewing the information in the browser, three message boxes will pop up one by one. The three different cases of different parameter numbers in the sayhi method are called.
In this case, the above method is similar to the method overload in C #. You can call your own branch based on different classes of the input parameter.
Note: it cannot be completely similar to the method overload of C #, because there is no way to call your own overload method based on the type of input parameter.
But now JS is developing so fast that it almost gets rid of the previous process-oriented programming and enters object-oriented programming. I believe this problem will be solved soon.