JSFiddle - React, Tailwind, and code Playground
by badsyntax
HTML
<!-- Click run above to execute the javascript -->
JavaScript
// Our super constructor
function Animal(name) {
this.name = name;
}
Animal.prototype.alertName = function() {
alert(this.name);
};
// Our sub constructor
function Cat(name) {
Animal.apply(this, arguments);
}
// Here we copy the animals prototype to the cats prototoype. (Note that Object.create is ECMAScript 5 and is not supported in older browsers.)
Cat.prototype = Object.create(Animal.prototype);
// An we add new methods
Cat.prototype.run = function() {
alert(this.name + ' is running');
};
var felix = new Cat('felix');
felix.alertName(); // inherited base (super) method
felix.run(); // sub method