Skip to content

Instantly share code, notes, and snippets.

@JoshCheek
Created July 9, 2018 05:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JoshCheek/a824326c4fbb080f98e018d1743e2efd to your computer and use it in GitHub Desktop.
Save JoshCheek/a824326c4fbb080f98e018d1743e2efd to your computer and use it in GitHub Desktop.
JS proxies to fix fn binding
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