example of overloading functions in JS

using variable length arguments list to effectively call diff't functions

by Gerald Gillespie

JavaScript

(function () {

    var multi = function () {
        var fns = {
            0: function () {
                console.log("0", this)
            },
            1: function (a) {
                console.log("1", a, this)
            },
            n: function () {
                console.log('default', this)
            }
        },
        l = fns[arguments.length] ? arguments.length : 'n';
        
        return fns[l].apply(this, arguments);
    };

    multi();
    multi('test');
})()