js的原型继承

<script>
    //动物(Animal),有头这个属性,eat方法
    //名字这个属性
    
    //猫有名字属性,继承Animal,抓老鼠方法

    function Animal(name){
        this.name = name;
    }
    
    Animal.prototype.eat = function(){
        console.log(this.name + " eating...");
    
    }
    
    function Cat(name){
        Animal.call(this,name);

    }
    Cat.prototype = Object.create(Animal.prototype);
    Cat.prototype.constructor = Cat;
    
    Cat.prototype.zhualaoshu = function(){
        console.log(this.name + " zhua laoshu");
    }
    
    var cat = new Cat("小猫");
    cat.zhualaoshu();
    
    
    
        
        
    </script>

 

posted @ 2017-11-27 15:24  Gold_Long  阅读(181)  评论(0)    收藏  举报