Created
May 30, 2021 21:07
-
-
Save misterpoloy/e08af35346460be5087966cb0a82c3a2 to your computer and use it in GitHub Desktop.
Polyfill for bind method
This file contains hidden or 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
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