1.0.10 • Published 5 years ago

@cherrypulp/mixin v1.0.10

Weekly downloads
1
License
MIT
Repository
gitlab
Last release
5 years ago

Mixin

codebeat badge Issues License npm version

In JavaScript we can only inherit from a single object. There can be only one [[Prototype]] for an object. And a class may extend only one other class. There’s a concept that can help here, called “mixins”.

Installation

npm install @cherrypulp/mixin

Quick start

Define a mixin

class FooTrait {
    static bar = 'bar';
    
    constructor() {
        this.baz = this.constructor.bar;
    }
    
    foo() {
        return this.baz;
    }
}

Use a mixin

class FooClass extends mixin(FooTrait) {}

const foo = new FooClass();
foo.foo(); // return "bar"

Be careful with names collision!

class FooClass extends mixin(FooTrait) {
    static bar = 'BAT';
    
    // or
    constructor() {
        super();
        this.baz = 'BAT';
    }
    
    // or
    foo() {
        return 'BAT';
    }
}

const foo = new FooClass();
foo.foo(); // return "BAT"

Versioning

Versioned using SemVer.

Contribution

Please raise an issue if you find any. Pull requests are welcome!

Author

License

This project is licensed under the MIT License - see the LICENSE file for details.

1.0.10

5 years ago

1.0.9

5 years ago

1.0.8

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago