Skip to content

Instantly share code, notes, and snippets.

@ryaninvents
Last active April 14, 2017 15:39
Show Gist options
  • Save ryaninvents/776d443031ea8bf65688891533db1dc8 to your computer and use it in GitHub Desktop.
Save ryaninvents/776d443031ea8bf65688891533db1dc8 to your computer and use it in GitHub Desktop.
Use ES7 double-colon bind while still referring to original "this"
class Client {
constructor(foo) {
this.name = foo
}
}
class Message {
constructor(content) {
this.content = content
}
get send() {
const self = this;
return function () {
console.log(`${this.name} says "${self.content}"`)
}
}
}
const client = new Client('World')
const message = new Message('Hello')
client::message.send() // or `message.send.call(client)` if the "`::`" syntax is not supported
// outputs `World says "Hello"`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment