大同市网站建设_网站建设公司_模板建站_seo优化
2025/12/26 15:30:05 网站建设 项目流程
function Animal(name) {this.name = name;
}Animal.prototype.speak = function() {console.log(this.name + ' makes a noise');
};function Dog(name, breed) {// 实现继承Animal.call(this,name);this.breed=breed;
}
//方法1 
// Dog.prototype=Object.create(Animal.prototype);
// Dog.prototype.constructor=Dog;
//方法二
inherit(Dog,Animal)
function inherit(child,parent){const proto=Object.create(parent.prototype);child.prototype=proto;child.prototype.constructor=child;}
Dog.prototype.bark=function(){console.log(this.name+ ' barks')
}// 实现 Dog 继承 Animal,并添加 bark 方法
const dog = new Dog('Buddy', 'Golden');
dog.speak(); // Buddy makes a noise
dog.bark();  // Buddy barks!
console.log(dog instanceof Dog); // true
console.log(dog instanceof Animal); // true

 

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询