Skip to content

Instantly share code, notes, and snippets.

@Znack
Created February 28, 2018 14:41
Show Gist options
  • Save Znack/3910e6729e8d8d6525eb047e15e35a84 to your computer and use it in GitHub Desktop.
Save Znack/3910e6729e8d8d6525eb047e15e35a84 to your computer and use it in GitHub Desktop.
Prototypes and Constructors
function Animal(name) {
this.name = name;
}
Animal.prototype.getName = function() {
return this.name;
}
Animal.prototype.sayHi = function() {
return "Hmmm...";
}
function Dog(name) {
Animal.call(this, name);
}
Dog.prototype = Object.create(Animal.prototype);
Dog.prototype.sayHi = function() {
return Animal.prototype.sayHi.call(this) + " Woof!!!";
}
Dog.prototype.makeHappy = function() {
return "Smile!";
}
function Cat(name) {
Animal.call(this, name);
}
Cat.prototype = Object.create(Animal.prototype);
Cat.prototype.makeHappy = function() {
return "Meow!";
}
var rex = new Dog("Rex");
console.log(rex.getName());
console.log(rex.sayHi());
var leopold = new Cat("Leopold");
console.log(leopold.getName());
console.log(leopold.sayHi());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment