JSFiddle - React, Tailwind, and code Playground
JavaScript
// Open console F12
// :) and hit Ctrl+Enter
// Read the code
var Base = (function(){
// Base definition
function Base(name){
this.name = name;
console.log("Base.constructor "+this.name);
}
Base.staticTest = function(){
console.log("Base.staticTest");
}
Base.prototype.proto = function(){
console.log("Base.prototype "+this.name);
}
return Base;
})();
var Trait1 = (function(){
// Trait1
function Trait1(){
console.log("Trait1.constructor");
}
Trait1.mixStatic = function(){
console.log("Trait1.mixStatic");
}
Trait1.prototype.mixProto = function(){
console.log("Trait1.mixProto");
}
return Trait1;
})();
var Trait2 = (function(){
// Trait2
function Trait2(){
console.log("Trait2.constructor");
}
Trait2.mixStatic2 = function(){
console.log("Trait2.mixStatic2");
}
Trait2.prototype.mixProto2 = function(){
console.log("Trait2.mixProto2");
}
return Trait2;
})();
var Child = (function(__super){
// !note
__extends(Child, __super);
// Child
function Child(name){
// call parent
__super.call(this, name);
// call tarit 1 constructor
Trait1.call(this);
// call trait 2 constructor
Trait2.call(this);
}
return Child;
})(Base);
var SubChild = (function(__super){
// !note
__extends(SubChild, __super);
// !note
__traits(SubChild, Trait1, Trait2);
// SubChild
function SubChild(name){
// Call parent constructoor
__super.call(this, name);
}
return SubChild;
})(Child);
// Test
var sc = new SubChild("Traits :) and inheritance");
sc.proto();
sc.mixProto();
sc.mixProto2();
SubChild.staticTest();
SubChild.mixStatic();
SubChild.mixStatic2();
// Helper function for inheritance
function __extends(d, b) {
for (var p in b){ if (b.hasOwnProperty(p)){ d[p] = b[p]; } };
function __() { this.constructor = d; }
__.prototype = b.prototype;
d.prototype = new __();
};
// Mix multiple classes as traits. Properties will...