Created
July 9, 2018 05:23
-
-
Save JoshCheek/a824326c4fbb080f98e018d1743e2efd to your computer and use it in GitHub Desktop.
JS proxies to fix fn binding
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
class Human { | |
constructor(name) { | |
this.name = name | |
} | |
greet(name) { | |
console.log(`Hi, ${name}, I'm ${this.name}!`) | |
} | |
} | |
Human = new Proxy(Human, { | |
construct: (fn, args) => new Proxy(new fn(...args), { | |
get: (o, p) => typeof o[p] === 'function' ? o[p].bind(o) : o[p] | |
}) | |
}) | |
const greet = new Human("Josh").greet | |
greet("Steve") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment