Skip to content

Instantly share code, notes, and snippets.

@misterpoloy
Created May 30, 2021 21:07
Show Gist options
  • Save misterpoloy/e08af35346460be5087966cb0a82c3a2 to your computer and use it in GitHub Desktop.
Save misterpoloy/e08af35346460be5087966cb0a82c3a2 to your computer and use it in GitHub Desktop.
Polyfill for bind method
const person = {
name: "Juan",
lastName: "Ortiz",
}
const printName = function() { // Important this can't be arrow function
console.log(`${this.name} ${this.lastName}`);
}
const printPerson = printName.bind(person);
printPerson();
// My custom implementation
Function.prototype.myBind = function(...args) {
const obj = this;
const params = args.slice(1); // this is for simplicity
return function() {
obj.apply(args[0], params);
}
}
const printPerson2 = printName.myBind(person);
printPerson2();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment