美文网首页
关于各种继承

关于各种继承

作者: tenro | 来源:发表于2020-06-11 12:30 被阅读0次

    原型链继承:

    子类构造函数指向父类的实例: 
            Son.prototype = new Parent()
            Son.prototype = Parent.__proto__
    

    构造函数继承:

     使用call, apply去调用父类的方法
            function Parent(){
                  this.name="dad"
                  this.obj={name:"Tenro"}
            }
            function Student(){
                   parent.call(this) 
            }
    

    组合继承:

    使用原型链加成后,再将构造行数consturctor指向自己的构造函数
    function Parent(){
          this.name="Tenro";
    }
    Parent.prototype.getName= function () {
          return this.name
    }
    
    function Son(){
         parent.call(this)  //调用父类
         this.realName="son"
    }
    Son.prototype = new Parent()  //将原型指向父类达到继承
    Son.constructor = Son //再将自己的构造函数指向自己

    相关文章

      网友评论

          本文标题:关于各种继承

          本文链接:https://www.haomeiwen.com/subject/nzxbtktx.html