What is the function signature in Javascript
Description
Arguments in Javascript function does not create a function signature.
Example
It's possible to simulate overloading by checking the type and number of arguments.
function doAdd() {/*from ww w .j ava 2 s .c om*/
if(arguments.length == 1) {
console.log(arguments[0] + 10);
} else if (arguments.length == 2) {
console.log(arguments[0] + arguments[1]);
}
}
doAdd(10); //20
doAdd(30, 20); //50
You can define functions which can accept any number of arguments and behave appropriately.
The code above generates the following result.
No Overloading
It's not overloading, but we can use it as a way to do overloading in Javascript.
Javascript functions cannot be overloaded in the traditional way.
If two functions have the same name, the last function wins.
function addSomeNumber(num){//from w w w . j a va2 s . c o m
return num + 1;
}
function addSomeNumber(num) {
return num + 2;
}
var result = addSomeNumber(1);
console.log(result);
The code above generates the following result.