其他分享
首页 > 其他分享> > 自定义继承

自定义继承

作者:互联网

4.4自定义继承

​ 【1】只更换一个对象的父对象

子对象._ _proto_ _=新父对象 //不推荐
           两句话作用完全一样
Object.setPrototypeOf(子对象, 新父对象) //推荐
        修改          子对象
                   的
             原型对象
                              为新父对象

​ 【2】批量更换多个子对象的父对象

只需要更换构造函数的prototype属性就可以

注意:必须在创建子对象之前更换,否则不会生效

    function Student(sname, sage){
      this.sname=sname;
      this.sage=sage;
    }//.prototype={         }
    
    var father={
      money:1000000000000,
      car:"infiniti"
    }
   // 批量更换多个子对象的父对象
    Student.prototype=father;
    
    var lilei=new Student("Li Lei",18)
    var hmm=new Student("Han Meimei",19);

    console.log(hmm);
    console.log(lilei);

    console.log(hmm.money, hmm.car);
    console.log(lilei.money, lilei.car);

标签:lilei,console,log,自定义,hmm,继承,对象,Student
来源: https://blog.csdn.net/qq_48648782/article/details/118444287