While I was just playing around with JS, I wrote following snippet:
function checkArgs(abc,nbn,jqrs){
console.log("overloaded " +arguments.length);
}
function checkArgs(abc){
console.log(arguments.length);
}
checkArgs("aa","lll","pp");
I see output as " 3 " , however I was expecting out put as "overloaded 3". But I does not happen , however if I just swap the postions of those method, it does happen.
function checkArgs(abc){
console.log(arguments.length);
}
function checkArgs(abc,nbn,jqrs){
console.log("overloaded " +arguments.length);
}
checkArgs("aa","lll","pp");
Whats the rationale behind it?
There is no function overloading in javascript.
The latest method declaration always overwrites the previous one with the same name. No error thrown.
Also functions in javascript are all variadic. Any number of arguments can be passed regardless of the function signature.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With