JSFiddle - React, Tailwind, and code Playground
by angstrey
HTML
<textarea id="output" rows="10" cols="80"></textarea>
JavaScript
var outputElement = document.getElementById("output");
var output = {};
output.writeln = function (obj) {
outputElement.value = [outputElement.value, obj.toString(), "\n"].join("");
return this;
};
//////////////
Function.prototype.inheritsFrom = function (parentClassOrObject) {
if (parentClassOrObject.constructor === Function) {
// Normal inheritance
this.prototype = new parentClassOrObject;
this.prototype.constructor = this;
this.prototype.parent = parentClassOrObject.prototype;
} else {
// Pure virtual inheritance
this.prototype = parentClassOrObject;
this.prototype.constructor = this;
this.prototype.parent = parentClassOrObject;
}
};
// "Pure Virtual Class"
var LivingBeing = {
name: "",
riseFromTheAshes: function () {
this.alive = true;
},
toString: function () {
return ["[LivingBeing ", this.name, "]"].join("");
}
};
Spirit.prototype = LivingBeing; // parent
Spirit.prototype.constructor = Spirit; // new constructor
Spirit.prototype.parent = LivingBeing;
Spirit.prototype.reproduce = function () {
};
function Spirit(name) {
this.name = name;
}
var angel = new Spirit("Casper");
output.writeln(angel);
//Mammal.prototype = LivingBeing;
//Mammal.prototype.constructor = Mammal;
//Mammal.prototype.parent = LivingBeing;
function Mammal(name) {
this.name = name || "";
this.offspring = [];
this.parent.riseFromTheAshes.call(this);
}
Mammal.inheritsFrom(LivingBeing);
Mammal.prototype.reproduce = function (cry) {
if (cry) {
cry = " - " + cry;
} else {
cry = "";
}
var babyName = "Baby " + this.name + cry;
var newBaby = new this.constructor(babyName);
this.offspring.push(newBaby);
return newBaby;
};
Mammal.prototype.toString = function () {
return ['[Mammal "', this.name, '"]'].join("");
};
//Cat.prototype = new Mammal();
//Cat.prototype.constructor =...