Skip to content

Instantly share code, notes, and snippets.

@thejh
Created July 18, 2011 12:13
Show Gist options
  • Save thejh/1089339 to your computer and use it in GitHub Desktop.
Save thejh/1089339 to your computer and use it in GitHub Desktop.
getUser example for method overloading example
getUser = (*) ->
(name) when typeof name is 'string' ->
User.lookup 'name', name
(id) when typeof id is 'number' ->
User.lookup 'id', id
->
throw new Error "invalid arguments"
getUser = function() {
if ((function(name) {
return typeof name === 'string';
})(arguments[0])) {
return (function(name) {
return User.lookup('name', name);
})(arguments[0]);
} else if ((function(id) {
return typeof id === 'number';
})(arguments[0])) {
return (function(id) {
return User.lookup('id', id);
})(arguments[0]);
} else {
throw new Error("invalid arguments");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment