Created
February 14, 2017 03:47
-
-
Save noodlehaus/6093e62f93e00441365da5f021d81214 to your computer and use it in GitHub Desktop.
detach methods as functions
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 detach(fn, cls) { | |
if (typeof cls.prototype[fn] !== 'function') { | |
throw new Error(fn + ' is not an instance function'); | |
} | |
return cls.prototype[fn].call.bind(cls.prototype[fn]); | |
} | |
var slice = detach('slice', Array); | |
var map = detach('map', Array); | |
slice([1, 2, 3], 1); // => [2, 3] | |
map([1, 2, 3], function (x) { return x*2 }); // => [2, 4, 6] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment