Method overloading in Javascript

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.