Created
March 20, 2012 22:07
-
-
Save fnando/2141765 to your computer and use it in GitHub Desktop.
Method 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
function overload(functions) { | |
return function() { | |
functions[arguments.length].apply(this, arguments); | |
} | |
} | |
function List() { | |
} | |
List.prototype.add = overload([ | |
function(){ console.log("no args", [this, arguments])}, | |
function(title){ console.log("1 arg", [this, arguments])}, | |
function(title, priority){ console.log("2 args", [this, arguments]);} | |
]); | |
var list = new List(); | |
list.add(); | |
list.add("Do something"); | |
list.add("Do something else", "high"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
é, o código do criador das funções ainda assim não fica bonito. não fica natural. a proposicao do EcmaScript 4 deixa o código mais leve de se ler (pelo menos pra mim, que não sei javascript).