@ampersarnie/implements

Add Traits and Interfaces to your javascript classes.

Usage no npm install needed!

<script type="module">
  import ampersarnieImplements from 'https://cdn.skypack.dev/@ampersarnie/implements';
</script>

README

Implements

Installation

npm install @ampersarnie/implements

Example

Interfaces

class MyInterface {
    myInterfaceMethod() {}
    anotherInterfaceMethod(foo, bar) {}
}

export default MyInterface

Traits

class MyTrait {
    myTraitMethod() {
        return 'Hello world.';
    }
}

export default MyTrait

Class

import { Implements } from '@ampersarnie/implements';
import MyInterface from './MyInterface.js';
import MyTrait from './MyTrait.js';

class MyClass {
    interfaces() {
        return [MyInterface];
    }

    traits() {
        return [MyTrait];
    }

    myInterfaceMethod() {
        // This is an standard interface method.
        // ...
        // Calling the trait method.
        return this.myTraitMethod();
    }

    anotherInterfaceMethod(foo, bar) {
        // This interface method requires the argument params
        // foo and bar as dictated by the interface.
    }

}

export default new Implements(MyClass);