Classiest
About
Classiest
allows you to write classier classes by providing the rich type-checking on arguments to methods through tcomb and overloadable constructors, methods, setters, and static methods.
Installation
npm i classiest
Usage
const {Classiest, tcomb: t} = require('classiest');
const Vector3 = Classiest('Vector3', (Vector3, Vector3Type) => ({
constructors: [
{
args: [t.Number, t.Number, t.Number],
fn: function (x, y, z) {
this.x = x;
this.y = y;
this.z = z;
}
},
{
args: [t.Number],
fn: function (n) {
this.x = this.y = this.z = n;
}
},
{
args: [],
fn: function () {
this.x = this.y = this.z = 0;
}
}
],
methods: {
add: [
{
args: [Vector3Type],
fn: function (v) {
return new Vector3(
this.x + v.x,
this.y + v.y,
this.z + v.z
)
}
},
{
args: [t.Number],
fn: function (n) {
return new Vector3(
this.x + n,
this.y + n,
this.z + n
)
}
}
],
dot: function (v) {
return this.x*v.x + this.y*v.x + this.z*v.z;
}
},
getters: {
length: function () {
return Math.sqrt(this.x**2 + this.y**2 + this.z**2);
}
},
setters: {},
statics: {
multiply: [
{
args: [Vector3Type, Vector3Type],
fn: function(v1, v2) {
return new Vector3(
v1.x * v2.x,
v1.y * v2.y,
v1.z * v2.z
);
}
},
{
args: [Vector3Type, t.Number],
fn: function(v1, n) {
return new Vector3(
v1.x * n,
v1.y * n,
v1.z * n
);
}
}
]
}
}));
const v1 = new Vector3(1, 2, 3);
const v2 = new Vector3(4);
const v3 = v1.add(v2);
const v4 = v1.add(1);
const vLength = v1.length;
const v5 = Vector3.multiply(v3, v4);
const v6 = Vector3.multiply(v3, vLength);