JSFiddle - React, Tailwind, and code Playground
by lukemartin
HTML
<div id='c'></div>
JavaScript
/* utils, ignore */
console.clear();
var d = function (msg) {document.getElementById('c').innerHTML += (msg + '<br />');};
/* /utils, ignore */
function Animal(name) {
this.name = name || '';
this.legs = 2;
this.fur = false;
return this;
};
Animal.prototype.setName = function (name) {
this.name = name;
return this;
};
Animal.prototype.setLegs = function (legs) {
this.legs = legs;
return this;
};
Animal.prototype.setFur = function (fur) {
this.fur = fur;
return this;
};
Animal.prototype.talk = function (words) {
d(this.getName() + ': ' + words);
return this;
};
Animal.prototype.getName = function () {
var name = this.name + ' (' + this.legs + ')';
if (this.fur) name = '<span style="color: ' + this.fur + '">' + this.name + ' (' + this.legs + ')' + '</span>';
return name;
};
Animal.prototype.imitate = function (animal) {
var faker;
// TODO find a better way of doing this
switch(animal.constructor.name) {
case 'Animal':
faker = new Animal(this.name);
break;
case 'Kitty':
faker = new Kitty(this.name);
break;
case 'Dog':
faker = new Dog(this.name);
break;
}
faker.setLegs(animal.legs).setFur(animal.fur).talk('balls');
delete faker;
faker = null; // how to delete?
return this;
};
var dave = new Animal('Dave').talk('Hello.');
function Kitty(name) {
Animal.call(this, name);
return this;
};
Kitty.prototype = new Animal;
Kitty.prototype.constructor = Kitty;
Kitty.prototype.talk = function (words) {
Animal.prototype.talk.call(this, 'meowwwwww!');
return this;
}
var whiskers = new Kitty('Whiskers').setLegs(4).setFur('orange').talk('meow');
dave.talk('Hello, ' + whiskers.getName());
whiskers.talk('Hello, ' + dave.getName());
function Dog(name ) {
Animal.call(this, name);
return this;
};
Dog.prototype = new Animal;
Dog.prototype.constructor = Dog;
var rex =...