JSFiddle - React, Tailwind, and code Playground

by IPWright83

JavaScript

const myInterface = Symbol('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();
            }
        };
    }
}


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);