Skip to content

Instantly share code, notes, and snippets.

@daytonn
Created November 26, 2013 19:33
Show Gist options
  • Save daytonn/7664690 to your computer and use it in GitHub Desktop.
Save daytonn/7664690 to your computer and use it in GitHub Desktop.
bind all implementation
var obj = {
id: 1,
whoami: function() {
return this.id;
}
};
function callExternally(callback) {
this.id = 99;
return callback();
}
function myBindAll() {
var args = Array.prototype.slice.call(arguments);
var context = args.shift();
_(args).each(function(func) {
var oldF = context[func];
context[func] = function() {
console.log("patched function");
return oldF.apply(context, arguments);
};
});
}
console.log(obj.id);
console.log(callExternally(obj.whoami));
console.log(myBindAll(obj, "whoami"));
console.log(callExternally(obj.whoami));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment