Javascript object-oriented overloading _ js object-oriented

Source: Internet
Author: User
In object-oriented languages, overloading is an important feature, while JavaScript, a self-proclaimed object-oriented language, does not directly provide overload functions. I have discussed the JavaScript object-oriented namespace, javascript object-oriented JavaScript classes, and JavaScript object-oriented private and public members in the previous sections.


Let me define it as follows:

The Code is as follows:


Function getDate (){.....}
Function getDate (date ){.....}


The next method will overwrite the previous one, although no error is reported.

But we can indeed implement overload. If you have used jQuery, you will have a deep understanding, such as $ ("# btn "). val () is the value of the button whose id is "btn", while $ ("# btn "). val ("Click me") is to assign a value to the button with id "btn.


So how can JavaScript be implemented (accurately speaking, it should be called "simulation ?.
The answer is simple: arguments
Arguments is a built-in object in JavaScript that contains the actual parameters passed by the caller, but it is not limited to the list of parameters defined in the function declaration, during the call, only the same length attribute as the array exists.
Let's take it as an "array" for the time being. We chose different implementations based on the length of the array and Its element types, thus simulating overload.
For details, see the following example:

The Code is as follows:


Function getDate (){
If (arguments. length = 0 ){
Var date = new Date (). toLocaleDateString ();
Return "You have not entered the parameter. Current Time:" + date;
}

If (arguments. length = 1 ){
If (arguments [0]. constructor = Date ){
Return "the parameter you entered is of the Date type, and the current time is:" + arguments [0]. toDateString ();
}
If (arguments [0]. constructor = String ){
Return "the parameter you entered is of the String type, and the current time is:" + arguments [0];
}
}

}


So we can call:

The Code is as follows:


GetDate ()
GetDate (new Date ())
GetDate ("Monday ")


In this way, JavaScript overload is implemented, but we find that this "Implementation" is too stubborn. If there are too many parameters, the code will be messy, if {...} is everywhere {...}. Therefore, I do not recommend using this overload in JavaScript.
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.