Skip to content

Instantly share code, notes, and snippets.

@gskachkov
Last active June 21, 2017 10:27
Show Gist options
  • Save gskachkov/3f9ba2ed92aa50e312fb66a7cf52f386 to your computer and use it in GitHub Desktop.
Save gskachkov/3f9ba2ed92aa50e312fb66a7cf52f386 to your computer and use it in GitHub Desktop.
const myInterface = Symbol('myInterface');
export default myInterface;
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;
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