Created
February 1, 2016 04:59
-
-
Save XGHeaven/cd1aa4ee5c7a7cab19ef to your computer and use it in GitHub Desktop.
JavaScript Magic Technology
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
/** | |
* 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