JSFiddle - React, Tailwind, and code Playground
by Tony C
JavaScript
function Animal(species) {
this.species = species;
}
// Create an accessor function on the prototype
Animal.prototype.getSpecies = function() {
return this.species;
}
function Dog(name, age) {
this.species = 'dog';
this.name = name;
this.age = age;
}
// Chain the prototype back to Animal
Dog.prototype = Object.create(Animal.prototype);
// Make sure the Dog constructor is called on instantiation
Dog.prototype.constructor = Dog;
// Add a new function to the prototype
Dog.prototype.call = function() {
console.log( "Here, " + this.name );
}
// Instantiate our new object
var myDog = new Dog('Fido', 4);
// Call each method on the object
myDog.call();
console.log(myDog.getSpecies());