原型继承-------------------

    function per(name,age){
        this.name=name;
        this.age=age;

    }
    per.prototype={
        id:10,
        class:'top',
    }
    function by(sex){
        this.sex=sex;
    }    
    by.prototype=new per('z2',20);
    var p1=new by();
    alert(p1.name);
    alert(p1.id+'\n'+p1.class);
//特点:即继承了父类的模板,又继承了父类的原型对象
//构造函数式继承
//只继承模板,不继承原型对象
类继承方式
function per(name,age){
        this.name=name;
        this.age=age;

    }
    per.prototype={
        id:10,
        class:'top',
    }
    function by(name,age,sex){
        per.call(this,name,age);
        this.sex=sex;
    }
    var p1=new by('张三',20,'男');
    
    alert(p1.name);
    //父类的原型对象没有继承过来
    alert(p1.id);   //undefined

原型继承+借用构造函数继承=混合继承

    //父类
    function per(name,age){
        this.name=name;
        this.age=age;

    }
    per.prototype={
        id:10,
        class:'top',
        say:function(){
            alert('吃');
        }
    }
    //子类
    function by(name,age,sex){
    //如果下面使用了原型继承by.prototype=new per();
    //这个就没有必要写了per.call(this,name,age);
        per.call(this,name,age); //继承父类的模板
        this.sex=sex;
    }
    //只剩下父类实例和父类原型对象的关系了,下面这句话就是为了继承父类原型的对象
    //原型继承方式
    by.prototype=new per();
    var p1=new by('张三',20,'男');
    alert(p1.name+'\n'+p1.id);

 

posted @ 2016-07-14 16:54  八bug哥哥  阅读(192)  评论(0编辑  收藏  举报