Skip to content

Instantly share code, notes, and snippets.

@fnando fnando/overload.js
Created Mar 20, 2012

Embed
What would you like to do?
Method overloading
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");
@peas

This comment has been minimized.

Copy link

commented Mar 20, 2012

é, 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).

@fnando

This comment has been minimized.

Copy link
Owner Author

commented Mar 20, 2012

Yep. Não gosto também.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.