js 的继承

  在 js 中 es6 新增了 class 然后通过 extends 来实现继承的方式,但其原理都是在 es5 的基础上实现的,在 es5 中大致存在这几种继承的方式:

  1、原型继承

    例如 function  dog () {

        this.classType = '犬科';

        this.className = '狗';

      }

      function yellowDog () {

        this.name = '黄狗';

      }

      yellowDog.prototype = new dog();

      此时,dog 就成为了 yellowDog 的原型,yellowDog 也具有了 classType 以及 className 的属性;

  2、构造继承

    构造继承是指在构造函数中去继承

    例如 function dog () { }

      function yellowDog () {

        dog.call(this);

        this.name = '黄狗

      }

      此时,dog 的 classType 等属性并不是作为 yellowDog 原型链的属性,而是成为了 yellowDog 的属性;

猜你喜欢

转载自www.cnblogs.com/mufc/p/10647693.html