Messing with prototypes

by theblang

JavaScript

let dog1 = {
	numLegs: 4
}

let dog2 = {
	numLegs: 4
}

let animal = {
	speak: function() {
  	console.log('Generic animal noise')
  }
}

Object.setPrototypeOf(dog1, animal)
Object.setPrototypeOf(dog2, animal)

console.log(dog1.speak())
console.log(dog2.speak())

function Animal() {}
Animal.prototype.speak = function() {
	console.log('Generic animal noise')
}

function Dog() {}
Dog.prototype.numLegs = 4

Object.setPrototypeOf(Dog, Animal)

let dog3 = new Dog();
console.log(dog3.numLegs);
console.log(dog3.speak());