Javascript - Function Function Overloading


Functions in ECMAScript don't have signatures, because the arguments are represented as an array.

Without function signatures, true overloading is not possible.

If two functions are defined to have the same name in ECMAScript, the last function becomes the owner of that name.

Consider the following example:

function addSomeNumber(num){
    return num + 100;

function addSomeNumber(num) {
    return num + 200;

var result = addSomeNumber(100);    //300

Here, the function addSomeNumber() is defined twice.

The first version of the function adds 100 to the argument, and the second adds 200.

When the last line is called, it returns 300 because the second function has overwritten the first.

It's possible to simulate overloading of methods by checking the type and number of arguments that have been passed into a function and then reacting accordingly.