// constructor function
function Animal(name) {
this.name = name
}
// add speak() method to Animal.prototype
// so they can be inherited
Animal.prototype.speak = function () {
console.log(`${this.name} can speak.`)
}
// create an Animal object
const animal = new Animal("Bruce")
console.log(animal)
animal.speak()
// constructor function
// that extends Animal
function Dog(name, breed) {
Animal.call(this, name)
this.breed = breed
}
// set Dog.prototype to inherit from Animal.prototype
Dog.prototype = Object.create(Animal.prototype)
Dog.prototype.constructor = Dog // reset the constructor property
// add bark() method to Dog.prototype
Dog.prototype.bark = function () {
console.log("Woof!")
}
// create a Dog object
const dog = new Dog("Max", "Lab")
console.log(dog)
dog.speak() // inherited from Animal
dog.bark() // Dog-specific method
To embed this project on your website, copy the following code and paste it into your website's HTML: