typescript-multiclass
v0.1.6
Published
Multiple inheritance for Javascript ES6
Downloads
207
Readme
Typescript Multiclass
typescript-multiclass
About
Multiple class inheritence like in Java.
This is a re-write of Panates - Multi Extend because the latest version is messed up in one way and the older version is messed up in another.
Installation
$ npm install typescript-multiclass [--save]
Usage
import classes from 'typescript-multiclass';
class Character {
constructor(name) {
this.canWalk = true;
this.distance = 0;
this.name = name;
}
walk(x) {
return this.distance += x;
}
}
class Jumper {
constructor() {
this.canJump = true;
this.distance = 0;
}
jump(x) {
return this.distance += x;
}
}
class Diver {
constructor() {
this.canDive = true;
this.deep = 0;
}
dive(x) {
return this.deep += x;
}
}
class SuperCharacter extends classes(Character, Jumper, Diver) {
}
const player = new SuperCharacter('player1');
console.log(player.name); // Prints out "player1"
if (player.canWalk) // Can walk and will walk 5
player.walk(5);
if (player.canJump) // Can jump and will jump 1
player.jump(1);
if (player.canDive) // Can dive and will dive 1
player.dive(1);
Argument pass-through
You can also define which arguments will be sent to subclass constructors
class SuperCharacter extends classes([Character, null], [Jumper, 1, 2], [Diver, [3, null]]) {
constructor(distanceMin, distanceMax, ...args) {
super(distanceMin, distanceMax, ...args);
this.name = 'player1';
}
}
In the example above Character constructor will be no argument, Jumper constructor will be called with arguments at index 1 and 2 (distanceMin, distanceMax), Diver constructor will be called with arguments at index from 3 to Last.
Checking implementations
You can check if extended class and its instance has implemented any class.
const classes = require('typescript-multiclass');
const Implemented = classes.Implemented; // Special symb ol
class SuperCharacter extends classes(Character, Jumper, Diver) {
}
if (SuperCharacter[Implemented](Jumper))
console.log('Jumper implemented');
// You can also check using class names
if (SuperCharacter[Implemented]('Diver'))
console.log('Diver implemented');
Node Compatibility
- node
>= 6.0
;