2.0.24 • Published 2 years ago

@blast-engine/mixable v2.0.24

Weekly downloads
51
License
-
Repository
-
Last release
2 years ago

mixable

Multiple inheritance library for JavaScript. It allows you to make classes that inherit from multiple parents.

Setup

npm install @blast-engine/mixable or yarn add @blast-engine/mixable

Usage:

MixableClasses can only inherit from other MixableClasses, and are created with createMixableClass()

Let's create an Animal class:

import { createMixableClass } from '@blast-engine/mixable'

const Animal = createMixableClass({
  name: 'Animal',
  body: class {
  
    static totalNumber() {
      return Animal.totalNumber
    }
  
    _constructor() {
      this.alive = false
      if (Animal.totalNumber) Animal.totalNumber++
      else Animal.totalNumber = 1
    }
    
    die() {
      this.alive = false 
    }
    
  }
})

We can instantiate it like a normal class.

const animal = new Animal()

console.log(shark.is(Animal)) // true
console.log(Animal.totalNumber()) // 1
console.log(animal.alive) // true

animal.die()
console.log(animal.alive) // false

Due to ECMAScript class limitations, we use _constructor() instead of constructor(). For the same reason, properties assigned in the class body will be ignored.

export const Animal = createMixableClass({
  name: 'Animal',
  body: class {
    
    // WRONG: will be ignored
    alive = false
    
    // WRONG: will be ignored
    constructor() {
      this.alive = false
    }
    
    // CORRECT
    _constructor() {
      this.alive = false
    }
    
  }
})

Now we can make a class Swimmer that inherits from Animal:

import { createMixableClass } from '@blast-engine/mixable'
import { Animal } from './Animal.class'

export const Swimmer = createMixableClass({
  name: 'Swimmer',
  inherits: [ Animal ],
  body: class {
  
    _constructor({ typeOfWater }) {
      this.typeOfWater = typeOfWater
    }
    
    getCaughtInNet() {
      this.die() // method inherited from Animal
    }
    
  }
})

When we instantiate it, both _constructor() functions are called.

console.log(Swimmer.inheritsFrom(Animal)) // true
console.log(Swimmer.inheritsFrom(Swimmer)) // true

const shark = new Swimmer({ typeOfWater: 'salty' })

console.log(shark.is(Animal)) // true
console.log(shark.is(Swimmer)) // true
console.log(shark.className()) // 'Swimmer'

console.log(shark.typeOfWater) // 'salty'
console.log(shark.alive) // true

shark.getCaughtInNet()
console.log(shark.alive) // false

We can inherit from multiple classes.

const { createMixableClass } = require('@blast-engine/mixable')
const { Swimmer } = require('./Swimmer.class')
const { Flyer } = require('./Flyer.class')

export const FlyingFish = createMixableClass({
  name: 'FlyingFish',
  inherits: [ Swimmer, Flyer ],
  body: class {
    
    avoidPredator() {
      this.swimToSurface() // inherited from Swimmer
      this.fly() // inherited from Flyer
      this.survive() // inherited from Animal
    }
    
  }
})
2.0.24

2 years ago

2.0.22

2 years ago

2.0.21

2 years ago

2.0.19

2 years ago

2.0.20

2 years ago

2.0.17

3 years ago

2.0.15

3 years ago

2.0.16

3 years ago

2.0.13

3 years ago

2.0.14

3 years ago

2.0.11

3 years ago

2.0.12

3 years ago

2.0.10

3 years ago

2.0.8

3 years ago

2.0.7

3 years ago

2.0.6

3 years ago

2.0.3

3 years ago

2.0.5

3 years ago

2.0.4

3 years ago

2.0.2

3 years ago

2.0.1

3 years ago

0.0.3

5 years ago

0.0.2

5 years ago