- 原型链继承
//定义一个动物类
function Animal (name){
this.name = name || 'animal';
this.sleep = function (){
console.log(this.name + '正在睡觉')
}
}
//原型方法
Animal.prototype.eat = function(food){
console.log(this.name + '正在吃东西'+ food)
}
//通过原型链继承
function Cat (){
}
Cat.prototype = new Animal();
Cat.prototype.name = 'cat';
cat =new Cat()
console.log(cat.eat('sth'))
console.log(cat.sleep())
- 借用构造函数继承