Created
June 19, 2014 16:35
-
-
Save srdjan/0d17b68356bcecf5cad5 to your computer and use it in GitHub Desktop.
javascript function overloading...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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