JSFiddle - React, Tailwind, and code Playground
JavaScript
//base constructor
mammal = function (spec) {
//public methods accessible to instances
this.get_name = function ( ) {
//private properties mean no prying
//eyes, only access is through API.
return spec.name;
};
this.says = function ( ) {
return spec.saying || '';
};
};
//How to reopen the constructor object??
//Which could be a more efficient way of opening
//an object and adding functionality to it?
mammal.prototype.count = function () {
var x = 5;
return this;
};
//extend base class
var cat = function (spec) {
//modify original spec.saying property
spec.saying = spec.saying || 'meow';
//run superclass constructor
mammal.call(this, spec);
//add an entirely new function available to children
this.purr = function (n) {
var i, s = '';
for (i = 0; i < n; i += 1) {
if (s) { s += '-'; } s += 'r'; }
return s;
};
//modify constructor method
this.get_name = function () {
return this.says() + ' ' + spec.name + ' ' + this.says();
};
};
var dog = function (spec) {
spec.saying = spec.saying || 'woof woof';
cat.call(this, spec);
};
myMammal = new mammal({name: 'Herb'});
myCat = new cat({name: 'Herb', saying: 'tings'});
myDog = new dog({name: 'Fred'});
alert(myMammal.get_name());
alert(myMammal.count());
alert(myCat.says());
alert(myDog.get_name());
alert(myDog.says());