面试题答案
一键面试function Animal(name) {
this.name = name;
}
Animal.prototype.speak = function() {
console.log(this.name +'makes a sound.');
};
function Dog(name) {
Animal.call(this, name);
}
Dog.prototype = Object.create(Animal.prototype);
Dog.prototype.constructor = Dog;
Dog.prototype.bark = function() {
console.log(this.name +'barks.');
};
// 创建Dog的实例
const myDog = new Dog('Buddy');
myDog.speak();
myDog.bark();