Skip to content

Instantly share code, notes, and snippets.

@XGHeaven
Created February 1, 2016 04:59
Show Gist options
  • Save XGHeaven/cd1aa4ee5c7a7cab19ef to your computer and use it in GitHub Desktop.
Save XGHeaven/cd1aa4ee5c7a7cab19ef to your computer and use it in GitHub Desktop.
JavaScript Magic Technology
/**
* call wrapper function is equal to use call/apply to invoke function
* see follow usage
*
* @param {Function} f - need to wrapper function
*/
var uncurryThis = function(f) {
var call = Function.call;
return function() {
return call.apply(f, arguments);
};
};
// This is equivalent, but slower
// uncurryThis = Function.bind.bind(Function.bind.call);
// http://jsperf.com/uncurrythis
// usage
var toString = uncurryThis(Object.prototype.toString)
toString(1); // '[object Number]'
toString('2'); // '[object String]'
toString({}); // '[object Object]'
// is equal to
Object.prototype.toString.call(1);
// or
Object.prototype.toString.apply(1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment