Last active
April 14, 2017 15:39
-
-
Save ryaninvents/776d443031ea8bf65688891533db1dc8 to your computer and use it in GitHub Desktop.
Use ES7 double-colon bind while still referring to original "this"
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
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