Skip to content

Instantly share code, notes, and snippets.

@guilhermegregio
Created January 21, 2014 18:31
Show Gist options
  • Save guilhermegregio/8545485 to your computer and use it in GitHub Desktop.
Save guilhermegregio/8545485 to your computer and use it in GitHub Desktop.
Override Method in Javascript
function addMethod(object, name, fn) {
var old = object[name];
object[name] = function () {
if (fn.length == arguments.length) {
return fn.apply(this, arguments);
} else if (typeof old == 'function') {
return old.apply(this, arguments);
}
}
}
var objeto = {
values: ['Camila', 'Viviane', 'Zacca'],
find : function(){return 'default';}
}
addMethod(objeto, 'find', function(){return this.values;});
addMethod(objeto, 'find', function(name){return this.values + name;});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment