Skip to content

Instantly share code, notes, and snippets.

@srdjan
Created June 19, 2014 16:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save srdjan/0d17b68356bcecf5cad5 to your computer and use it in GitHub Desktop.
Save srdjan/0d17b68356bcecf5cad5 to your computer and use it in GitHub Desktop.
javascript function overloading...
var get = (function() {
var overrides = {
getAll: function() {
console.log('return all...');
return this;
},
getById: function(args) {
console.log('return by id:' + args);
return this;
},
getByExpr: function(args) {
console.log('return by expression: ' + args);
return this;
}
}
return function () {
var args = [].slice.call(arguments, 0);
if (args.length === 0) {
return overrides['getAll'].apply(this, args);
}
if (args.length === 1) {
if (typeof args[0] === 'string') {
return overrides['getByExpr'].apply(this, args);
}
if (typeof args[0] === 'number') {
return overrides['getById'].apply(this, args);
}
throw "invalid argument";
}
};
}());
get();
get(5);
get('id > 5');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment