Last active
June 1, 2016 10:06
-
-
Save friedemannsommer/4ec53e3b63eee6521805 to your computer and use it in GitHub Desktop.
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
function bind(fn, context) { | |
var givenArguments = []; | |
for (var _i = 2; _i < arguments.length; _i++) { | |
givenArguments[_i - 2] = arguments[_i]; | |
} | |
if (typeof Function.prototype.bind == "function") { | |
return Function.prototype.bind.apply(fn, Array.prototype.concat.apply([context], givenArguments)); | |
} | |
return (givenArguments.length > 0) | |
? function () { | |
var localArguments = []; | |
for (var _i = 0; _i < arguments.length; _i++) { | |
localArguments[_i - 0] = arguments[_i]; | |
} | |
return (localArguments.length > 0) | |
? fn.apply(context, Array.prototype.concat.apply(givenArguments, localArguments)) | |
: fn.apply(context, givenArguments); | |
} | |
: function () { | |
var localArguments = []; | |
for (var _i = 0; _i < arguments.length; _i++) { | |
localArguments[_i - 0] = arguments[_i]; | |
} | |
return (localArguments.length > 0) | |
? fn.apply(context, localArguments) | |
: fn.apply(context); | |
}; | |
} |
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
function bind(fn: Function, context: Object, ...givenArguments: Array<any>): Function { | |
if (typeof Function.prototype.bind == "function") { | |
return Function.prototype.bind.apply(fn, Array.prototype.concat.apply([context], givenArguments)); | |
} | |
return (givenArguments.length > 0) | |
? (...localArguments: Array<any>) => { | |
return (localArguments.length > 0) | |
? fn.apply(context, Array.prototype.concat.apply(givenArguments, localArguments)) | |
: fn.apply(context, givenArguments) | |
} | |
: (...localArguments: Array<any>) => { | |
return (localArguments.length > 0) | |
? fn.apply(context, localArguments) | |
: fn.apply(context) | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment