Last active
December 20, 2015 17:09
-
-
Save dperrymorrow/6166957 to your computer and use it in GitHub Desktop.
javascript inheritance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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