Skip to content

Instantly share code, notes, and snippets.

@paul-bjorkstrand
Created March 4, 2014 22:52
Show Gist options
  • Save paul-bjorkstrand/9357434 to your computer and use it in GitHub Desktop.
Save paul-bjorkstrand/9357434 to your computer and use it in GitHub Desktop.
(function() {
if (!Function.prototype.callFn) {
Function.prototype.callFn = function(scope/*, args */) {
var args = Array.prototype.slice.call(arguments, 1);
return this.applyFn(scope, args);
};
}
if (!Function.prototype.applyFn) {
Function.prototype.applyFn = function(scope, args) {
var fn = this;
return function closurized() {
return fn.apply(scope, args);
}
};
}
if (!Function.prototype.lcurry) {
Function.prototype.lcurry = function(scope/*, args */) {
var args = Array.prototype.slice.call(arguments, 1);
return this.lcurryApply(scope, args);
};
}
if (!Function.prototype.rcurry) {
Function.prototype.rcurry = function(scope/*, args */) {
var args = Array.prototype.slice.call(arguments, 1);
return this.rcurryApply(scope, args);
};
}
if (!Function.prototype.lcurryApply) {
Function.prototype.lcurryApply = function(scope, args) {
var fn = this;
return function() {
var newArgs = Array.prototype.slice.call(arguments, 0);
return fn.apply(scope, args.concat(newArgs));
}
};
}
if (!Function.prototype.rcurryApply) {
Function.prototype.rcurryApply = function(scope, args) {
var fn = this;
return function() {
var newArgs = Array.prototype.slice.call(arguments, 0);
return fn.apply(scope, newArgs.concat(args));
}
};
}
})();
(function() {
function logStr(str) {
console.log(str);
}
function log2Str(str1, str2) {
console.log(str1, str2);
}
var callLogStr = logStr.callFn(null, "a");
var applyLogStr = logStr.applyFn(null, ["b"]);
var lcurryLog2Str = log2Str.lcurry(null, "a");
var rcurryLog2Str = log2Str.rcurry(null, "a");
callLogStr();
applyLogStr();
lcurryLog2Str("b");
rcurryLog2Str("b");
Function.prototype.callFn = undefined;
Function.prototype.applyFn = undefined;
Function.prototype.lcurry = undefined;
Function.prototype.lrcurry = undefined;
Function.prototype.lcurryApply = undefined;
Function.prototype.lrcurryApply = undefined;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment