Skip to content

Instantly share code, notes, and snippets.

@noodlehaus
Created February 14, 2017 03:47
Show Gist options
  • Save noodlehaus/6093e62f93e00441365da5f021d81214 to your computer and use it in GitHub Desktop.
Save noodlehaus/6093e62f93e00441365da5f021d81214 to your computer and use it in GitHub Desktop.
detach methods as functions
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