// 定义一个动物类
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';
// 创建猫的实例
var cat = new Cat()
// 此时cat也会具有animal的属性
// (二)构造函数继承
function Cat(name){
Animal.call(this);
this.name = name || 'Tom';
}