Created
March 26, 2018 12:48
-
-
Save abinavseelan/d856ee56796763362e967995b022e1a4 to your computer and use it in GitHub Desktop.
Implementing .bind with .apply
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
/* | |
Question: Assume .bind does not exist. | |
Implement the .bind functionality using .apply() | |
*/ | |
Function.prototype.bind = function(thisArg) { | |
// Get a reference to the function that you want to bind | |
let functionToBind = this; | |
return function() { | |
/* | |
Return a function that, when called, will internally call the | |
functionToBind, applied with the this context of thisArg. | |
*/ | |
functionToBind.apply(thisArg, arguments); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment