Skip to content

Instantly share code, notes, and snippets.

@abinavseelan
Created March 26, 2018 12:48
Show Gist options
  • Save abinavseelan/d856ee56796763362e967995b022e1a4 to your computer and use it in GitHub Desktop.
Save abinavseelan/d856ee56796763362e967995b022e1a4 to your computer and use it in GitHub Desktop.
Implementing .bind with .apply
/*
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