Created
November 26, 2013 19:33
-
-
Save daytonn/7664690 to your computer and use it in GitHub Desktop.
bind all implementation
This file contains hidden or 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
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