zoukankan      html  css  js  c++  java
  • JS实现继承的几种方式

    借用构造函数实现继承

    1

    2

    3

    4

    5

    6

    7

    function Parent1(){

    this.name = "parent1"

    }

    function Child1(){

    Parent1.call(this);

    this.type = "child1";

    }

     

    缺点:Child1无法继承Parent1的原型对象,并没有真正的实现继承(部分继承)

    借用原型链实现继承

    1

    2

    3

    4

    5

    6

    7

    8

    function Parent2(){

    this.name = "parent2";

    this.play = [1,2,3];

    }

    function Child2(){

    this.type = "child2";

    }

    Child2.prototype = new Parent2();

    缺点:原型对象的属性是共享的

    组合式继承  

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    function Parent3(){

    this.name = "parent3";

    this.play = [1,2,3];

    }

    function Child3(){

    Parent3.call(this);

    this.type = "child3";

    }

    Child3.prototype = Object.create(Parent3.prototype);

    Child3.prototype.constructor = Child3;

    重拾梦想 改写人生
  • 相关阅读:
    Annotation
    jdbc
    集合
    与运行环境交互
    计数排序and基数排序
    面向对象下
    面向对象
    流程控制与数组
    庆祝自己的BLOG开张
    Raspberry Pi配置为无线路由器
  • 原文地址:https://www.cnblogs.com/wulibo/p/7596698.html
Copyright © 2011-2022 走看看