Last active
June 21, 2017 10:27
-
-
Save gskachkov/3f9ba2ed92aa50e312fb66a7cf52f386 to your computer and use it in GitHub Desktop.
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 myInterface = Symbol('myInterface'); | |
export default myInterface; |
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
import myInterface from 'myInterface'; | |
class Foo { | |
constructor(value) { | |
this.value = value; | |
} | |
_inc () { | |
this.value = this.value + 1; | |
} | |
_getValue () { | |
return this.value; | |
} | |
get [myInterface]() { | |
return { | |
inc: this._inc, | |
get value() { | |
return this._getValue(); | |
} | |
}; | |
} | |
} | |
export default A; |
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
import myInterface from 'myInterface'; | |
import Foo from 'foo'; | |
function doInc(obj){ | |
const interface = obj[myInterface]; | |
if (interface === undefined) { | |
throw new Error('Object does not support the interface.'); | |
} | |
interface.inc(); | |
} | |
const counter = new Foo(0); | |
doInc(counter); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment