DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Nikola has posted 1 posts at DZone. View Full User Profile

JavaScript function overloading

04.10.2013
| 5188 views |
  • submit to reddit
var def = function(functions, parent) {
 return function() {
    var types = [];
    var args = [];
    eachArg(arguments, function(i, elem) {
        args.push(elem);
        types.push(whatis(elem));
    });
    if(functions.hasOwnProperty(types.join())) {
        return functions[types.join()].apply(parent, args);
    } else {
        if (typeof functions === 'function')
            return functions.apply(parent, args);
        if (functions.hasOwnProperty('default'))
            return functions['default'].apply(parent, args);        
    }
  };
};

var eachArg = function(args, fn) {
 var i = 0;
 while (args.hasOwnProperty(i)) {
    if(fn !== undefined)
        fn(i, args[i]);
    i++;
 }
 return i-1;
};

var whatis = function(val) {

 if(val === undefined)
    return 'undefined';
 if(val === null)
    return 'null';

 var type = typeof val;

 if(type === 'object') {
    if(val.hasOwnProperty('length') && val.hasOwnProperty('push'))
        return 'array';
    if(val.hasOwnProperty('getDate') && val.hasOwnProperty('toLocaleTimeString'))
        return 'date';
    if(val.hasOwnProperty('toExponential'))
        type = 'number';
    if(val.hasOwnProperty('substring') && val.hasOwnProperty('length'))
        return 'string';
 }

 if(type === 'number') {
    if(val.toString().indexOf('.') > 0)
        return 'float';
    else
        return 'int';
 }

 return type;
};

var out = def({
    'int': function(a) {
        alert('Here is int '+a);
    },

    'float': function(a) {
        alert('Here is float '+a);
    },

    'string': function(a) {
        alert('Here is string '+a);
    },

    'int,string': function(a, b) {
        alert('Here is an int '+a+' and a string '+b);
    },
    'default': function(obj) {
        alert('Here is some other value '+ obj);
    }

});

out('ten');
out(1);
out(2, 'robot');
out(2.5);
out(true);

Comments

Victor Arnold replied on Sat, 2014/02/08 - 4:07pm

 The first aspect to consider is staff turnover, because a low turnover rate indicates that management recognizes and invests in quality technicians. Life Experience Degrees

Jose Jones replied on Sat, 2013/12/28 - 8:07am

It might be difficult to find out which specific person will be working on the truck, but it is still possible to get a feel for the overall workforce. The first aspect to consider is staff turnover, because a low turnover rate indicates that management recognizes and invests in quality technicians. One should also ask about how technically proficient or trained employees are, which could be determined by asking what kind of training programs the company uses for new hires. Keep in mind there is no substitute for experience, so one should ask how many average years of experience an organization's technicians have.

Trucking Hawaii 

Perry Kate replied on Thu, 2013/09/05 - 7:12pm

Sagive Seo replied on Fri, 2013/06/21 - 5:17pm

Share an example where i might need to use such function?

Neev Kidman replied on Mon, 2013/06/17 - 5:31pm

I studied JavaScript to make strong my web development skills. I was always very week in function overloading of JavaScript. But when I read it then my all confusions have been vanished. So whenever any students comes to me to buy dissertation UK  made on JavaScript then I can help him now.

Perry Kate replied on Thu, 2013/06/13 - 9:00pm

Shushan Ele replied on Mon, 2013/05/13 - 8:51am

 Our highly qualified and highly skilled essay writer uk   are available. Avail their finest services now and enjoy guaranteed A+. Our writers will write a genuine. The internet is awash with people offering to write essays for students. Get essay help from our essay writers to ensure that your essay is the best.