Javascript inheritance
function Animal(name) {
this.name = name;
this.breathe = function() {
// do something
}
}
function Dog(name) {
Animal.call(this, name);
this.bark = function() {
// do something
}
}
Dog.prototype = new Animal();
Dog.prototype.constructor = Dog;
var dog = new Dog("Rex");
console.log(dog instanceof Dog);
console.log(dog instanceof Animal);
console.log(Dog.prototype.constructor);
console.log(Dog.name);Written by Hercules Merscher
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Javascript
Authors
Did you know?
JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.
Security insights from Cside
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#