If I define it like this:
function getDate(){.....}
function getDate(date){.....}
Then the latter method will overwrite the previous one, although there is no error.
But we can indeed implement overloading. If you have used jQuery, you will have a deep understanding. For example, $("#btn").val() is to get the value of the button with id "btn" and $("#btn").val("click me") is to assign a value to the button with id "btn".
So how is JavaScript implemented (to be precise, 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 is not limited to the parameter list defined by the function declaration. When called, it only has a length attribute like the array.
Let's understand it as an "array". We select different implementations based on the length of the array and the type of its elements, thus simulated overloading.
For details, please see the following example:
function getDate(){
if(==0){
var date=new Date().toLocaleDateString();
return "You did not enter the parameter, now time:"+date;
}
if(==1){
if(arguments[0].constructor ==Date){
return "The parameter you entered is Date type, and the current time is: "+arguments[0].toDateString();
}
if(arguments[0].constructor ==String){
return "The parameter you entered is of String type, and the current time is: "+arguments[0];
}
}
}
So we can call this:
getDate()
getDate(new Date())
getDate("Monday")
This implements JavaScript overloading, but we find that such "implementation" is too forced. If there are too many parameters, it will appear to be incompetent, the code will be very messy, and there are if{...} everywhere. So I don't recommend using such overloads in JavaScript.
Copy the codeThe code is as follows:
function getDate(){.....}
function getDate(date){.....}
Then the latter method will overwrite the previous one, although there is no error.
But we can indeed implement overloading. If you have used jQuery, you will have a deep understanding. For example, $("#btn").val() is to get the value of the button with id "btn" and $("#btn").val("click me") is to assign a value to the button with id "btn".
So how is JavaScript implemented (to be precise, 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 is not limited to the parameter list defined by the function declaration. When called, it only has a length attribute like the array.
Let's understand it as an "array". We select different implementations based on the length of the array and the type of its elements, thus simulated overloading.
For details, please see the following example:
Copy the codeThe code is as follows:
function getDate(){
if(==0){
var date=new Date().toLocaleDateString();
return "You did not enter the parameter, now time:"+date;
}
if(==1){
if(arguments[0].constructor ==Date){
return "The parameter you entered is Date type, and the current time is: "+arguments[0].toDateString();
}
if(arguments[0].constructor ==String){
return "The parameter you entered is of String type, and the current time is: "+arguments[0];
}
}
}
So we can call this:
getDate()
getDate(new Date())
getDate("Monday")
This implements JavaScript overloading, but we find that such "implementation" is too forced. If there are too many parameters, it will appear to be incompetent, the code will be very messy, and there are if{...} everywhere. So I don't recommend using such overloads in JavaScript.