Prototypal Inheritance

Describing it to LP.

by jcreamer898

JavaScript

// Animal Constructor
function Animal() {
   this.speak();
}

Animal.prototype.speak = function() {
  console.log("speak");
};

// console.log(Animal, Animal.prototype, Animal.prototype.constructor);

// console.log(Animal === Animal.prototype.constructor);

// Dog Constructor
function Dog() {
  Animal.apply(this, arguments);
}

// Setup Inheritance
Dog.prototype = Object.create(Animal.prototype);
Dog.prototype.constructor = Dog; // Don't forget your a dog

Dog.prototype.speak = function() {
  console.log("bark");
};

// Dog Constructor
function Yorkie() {
  Animal.apply(this, arguments);
}

// Setup Inheritance
Yorkie.prototype = Object.create(Dog.prototype);
Yorkie.prototype.constructor = Yorkie; // Don't forget your a dog

Yorkie.prototype.speak = function() {
  console.log("little bark");
};

// console.log("**Dog**");
// console.log(Dog.prototype);

function Cat() {
  Animal.apply(this, arguments);
}

// Cat is a type of an animal
Cat.prototype = Object.create(Animal.prototype);
Cat.prototype.constructor = Cat;

//Cat.prototype.speak = function() {
//  console.log("meow");
//};

// console.log(Cat.prototype, Cat.prototype.constructor);

var animal = new Animal(); // speak
var dog = new Dog();
var yorkie = new Yorkie();

//console.log(Object.getPrototypeOf(dog));

console.log(animal, dog, yorkie);