For Javascript in small projects, you only need to write a few functions. However, in large projects, especially in developing websites that require good user experience, such as SNS, a large amount of javascrpt will be used. Sometimes the Javascript workload is better than C #, at this time, writing a bunch of functions will become messy, messy, and even name conflicts, making management and maintenance troublesome. In this case, we need to use the object-oriented idea to develop JavaScript. Let's do this:
In object-oriented languages, overloading is an important feature, while JavaScript, a self-proclaimed object-oriented language, does not directly provide overload functions.
Let me define it 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:
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. The current time is: " + Arguments [ 0 ];
}
}
}
So we can call: Getdate ()
Getdate (NewDate ())
Getdate ("Monday")
In this way, JavaScript overload is implemented, but we find that this "Implementation" is too stubborn. If there are too many parameters, it will not work,CodeIt will also be messy. If {...} is everywhere {...}. Therefore, I do not recommend using this overload in JavaScript.
Address: http://www.cnblogs.com/xumingxiang/archive/2010/04/30/1725410.html
Author: Xu mingxiang
Source: http://www.cnblogs.com/xumingxiang
Copyright: The copyright of this article is shared by the author and the blog
Reprinted: you are welcome to reprinted. To save the author's Creative Enthusiasm, please [reprinted] As required. Thank you.
Requirement: This statement must be retained without the author's consent; Article The original text connection is provided; otherwise, the legal liability is required.