JSFiddle - React, Tailwind, and code Playground
by Aaron Zhang
JavaScript
(function () {
'use strict';
Object.prototype.inherit = function (namespaces, parents) {
var prototypes = {};
if (namespaces.length != parents.length) {
throw 'You need same number of namespaces and classes';
}
var newSet = {};
var distinct = {};
for (var i = 0; i < namespaces.length; ++i) {
// store parent prototypes
prototypes[namespaces[i]] = parents[i].prototype;
for (var k in parents[i].prototype) {
var func = parents[i].prototype[k];
if (!this.prototype[k]) {
if (!distinct[k]) {
distinct[k] = func; //function only exists in one parent class
} else {
delete distinct[k]; //function exists in more than one parent class
}
}
}
for (var k in distinct) { //attach to current class
this.prototype[k] = distinct[k];
}
}
this.prototype.$super = function (name) {
return prototypes[name];
};
};
var Father = function () {
console.log('Father Init');
this.father = true;
};
Father.prototype.sayHi = function () {
console.log('Hi from Father');
};
Father.prototype.sayBye = function () {
console.log('bye!');
};
function Mother() {
console.log('Mother Init');
this.mother = true;
}
Mother.prototype.sayHi = function () {
console.log('Hi from Mother');
};
function Child() {
Father.apply(this, arguments);
Mother.apply(this, arguments);
console.log('Child Init');
}
Child.inherit(['Father', 'Mother'], [Father, Mother]);
function GrandChild() {
Child.apply(this, arguments);
console.log('GrandChild Init');
}
GrandChild.inherit(['Child'], [Child]);
var gc = new...