Skip to content

Instantly share code, notes, and snippets.

@dperrymorrow
Last active December 20, 2015 17:09
Show Gist options
  • Save dperrymorrow/6166957 to your computer and use it in GitHub Desktop.
Save dperrymorrow/6166957 to your computer and use it in GitHub Desktop.
javascript inheritance
function Animal (name) {
console.log("Animal constructor is called " + name);
}
Animal.prototype.sleep = function () {
console.log(this.name + ' is sleeping');
};
// extending the parent classe
function Cat (name) {
console.dir(this.__proto__)
this.name = name;
// this.__proto__ = new Bar();
}
Cat.prototype = Animal.prototype;
Cat.prototype.constructor = Cat;
Cat.prototype.catchMice = function () {
console.log(this.name + ' is catching mice');
};
// use the class
var cat = new Cat("Patches");
cat.sleep();
cat.catchMice();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment