1. // 定义一个动物类
    2. function Animal (name) {
    3. // 属性
    4. this.name = name || 'Animal';
    5. // 实例方法
    6. this.sleep = function(){
    7. console.log(this.name + '正在睡觉!');
    8. }
    9. }
    1. // (一) 原型方法
    2. Animal.prototype.eat = function(food) {
    3. console.log(this.name + '正在吃:' + food);
    4. };
    5. // 定义一个猫
    6. function Cat(){
    7. }
    8. // 通过对猫类的原型集成动物的类
    9. Cat.prototype = new Animal();
    10. Cat.prototype.name = 'cat';
    11. // 创建猫的实例
    12. var cat = new Cat()
    13. // 此时cat也会具有animal的属性
    1. // (二)构造函数继承
    2. function Cat(name){
    3. Animal.call(this);
    4. this.name = name || 'Tom';
    5. }