Skip to content

Instantly share code, notes, and snippets.

@lamchau
Created July 8, 2015 18:37
Show Gist options
  • Save lamchau/47bc78cafef6218a7b6c to your computer and use it in GitHub Desktop.
Save lamchau/47bc78cafef6218a7b6c to your computer and use it in GitHub Desktop.
function demethodize(fn) {
if (typeof fn === "function") {
return Function.bind.bind(Function.call)(fn);
}
return null;
}
_.mixin((function() {
return {
demethodize: function(fn) {
return _.bind(_.call, fn);
}
};
})());
function testDemethodize(input, fn) {
var result = Array.isArray(input) ? input.map(fn) : fn.call(null, null, input);
console.log('result', result);
}
testDemethodize(['a', 'b', 'c'], _.demethodize(String.prototype.toUpperCase));
testDemethodize(['a', 'b', 'c'], demethodize(String.prototype.toUpperCase));
testDemethodize({ a: 1 }, _.demethodize(Object.keys));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment