寄生组合继承

function Animal(name) {
​  this.name = name;
​}
​​Animal.prototype.speak = function() {
​  console.log(this.name + ' makes a sound');​
};​​
function Dog(name, breed) {​
  // 使用构造函数继承,继承属性​
  Animal.call(this, name);
​  this.breed = breed;
​}
​​// 使用 Object.create 继承原型
​Dog.prototype = Object.create(Animal.prototype);​
Dog.prototype.constructor = Dog; // 修复 constructor 引用
​​Dog.prototype.speak = function() {
​  console.log(this.name + ' barks');​
};​​
var myDog = new Dog('Buddy', 'Golden Retriever');​
myDog.speak(); // 输出 "Buddy barks"​​
posted @ 2025-10-11 16:44  阿木隆1237  阅读(5)  评论(0)    收藏  举报