美文网首页
js-原型实现继承

js-原型实现继承

作者: YellowPoint | 来源:发表于2019-07-11 17:29 被阅读0次
    /*
      1.借助构造函数
    */
    function Parent1() {
      this.name = 'parent'
    }
    Parent1.prototype.say = function () {

    }

    function Child1() {
      Parent1.call(this)
      this.type = 'child'
    }
    console.log(new Child1())
    // 缺点是无法继承原型链上的

    /*
      2.借助原型链
    */
    function Parent2() {
      this.name = 'parent2'
      this.arr = [1, 2, 3]
    }
    Parent2.prototype.say = function () {

    }

    function Child2() {
      this.type = 'child2'

    }
    Child2.prototype = new Parent2()
    console.log(new Child2())
    let child2_1 = new Child2()
    let child2_2 = new Child2()
    child2_1.arr.push(4)
    console.log(child2_1.arr, child2_2.arr)
    // 缺点是new出来的实例,父元素的属性没有隔离


    /*
      3.组合 new Parent3()
    */
    function Parent3() {
      this.name = 'parent3'
      this.arr = [1, 2, 3]
    }
    Parent3.prototype.say = function () {

    }

    function Child3() {
      Parent3.call(this)
      this.type = 'child3'
    }
    Child3.prototype = new Parent3()
    console.log(new Child3())
    let child3_1 = new Child3()
    let child3_2 = new Child3()
    child3_1.arr.push(4)
    console.log(child3_1.arr, child3_2.arr)
    // 缺点是 Parent3执行了两次

    /*
      4.组合优化Parent4.prototype
    */
    function Parent4() {
      this.name = 'parent4'
      this.arr = [1, 2, 3]
    }
    Parent4.prototype.say = function () {

    }

    function Child4() {
      Parent4.call(this)
      this.type = 'child4'
    }
    Child4.prototype = Parent4.prototype
    // Child4.prototype.constructor = Child4
    console.log(new Child4())
    let child4_1 = new Child4()
    let child4_2 = new Child4()
    child4_1.arr.push(4)
    console.log(child4_1.arr, child4_2.arr)
    console.log(child4_1.constructor === Child4, child4_1.constructor === Parent4)
    // 这里的Child4就是一个继承自Function的函数对象
    console.log(Child4.constructor === Parent4) //false
    // Child4.__proto__ === Function.prototype
    // true
    // Child4.constructor === Function
    // true
    let parent4 = new Parent4
    console.log(parent4.constructor === Parent4) //true
    /*
      还存在的问题  实例constructor指向问题
      由于将Child4的prototype指向了Parent4的prototype
      导致 child4_1.constructor===Child4 false
          child4_1.constructor===Parent4 true

    
    */
    /*
      5. 组合再优化 create
    */

    function Parent5() {
      this.name = 'parent5'
      this.arr = [1, 2, 3]
    }


    function Child5() {
      Parent5.call(this)
      this.type = 'child5'
    }
    Child5.prototype = Object.create(Parent5.prototype)
    // 回忆 Object.create的原理,是将创建的新对象的prototype指向传入的元素
    Child5.prototype.constructor = Child5
    Parent5.prototype.say = function () {

    }
    // 在上面的方法中,加上这一行也不行,因为 Child 和 Parent 的prototype指向同一个地方,给Child添加constructor属性会导致Parent的constructor也被修改了
    console.log(new Child5())
    let child5_1 = new Child5()
    let child5_2 = new Child5()
    child5_1.arr.push(4)
    console.log(child5_1.arr, child5_2.arr)
    console.log(child5_1.constructor === Child5, child5_1.constructor === Parent5)


    /*
      6. 用assign试试呢
    */

    function Parent6() {
      this.name = 'parent6'
      this.arr = [1, 2, 3]
    }


    function Child6() {
      Parent6.call(this)
      this.type = 'child6'
    }
    Child6.prototype = Object.assign({
      constructor: Child6
    }, Parent6.prototype)
    Parent6.prototype.say = function () {

    }
    console.log(new Child6())
    let child6_1 = new Child6()
    let child6_2 = new Child6()
    child6_1.arr.push(4)
    console.log(child6_1.arr, child6_2.arr)
    console.log(child6_1.constructor === Child6, child6_1.constructor === Parent6)
    let parent6 = new Parent6
    console.log(parent6.constructor === Parent6) //true
    // 这个的缺点在于 Parent.prototype在assign之后添加方法的话child6_1就拿不到了
    // 而create还可以是因为那是一个引用


相关文章

  • js-原型实现继承

  • js基础之实现继承的几种方式

    js 实现继承的方式有: 原型链继承; 构造函数继承; 组合继承(原型链继承 + 构造函数继承)(最常用);(原型...

  • 2019-03-25 继承

    js中通过原型来实现继承 组合继承:原型继承+借用构造函数继承

  • 关于继承

    继承的实现方法 属性拷贝 浅拷贝 深拷贝 原型继承 原型式继承 原型链继承 组合继承(深拷贝+优化后的原型式继承)...

  • 十三(7)、面向对象之原型继承 ------ 2020-01-0

    1、例子: 2、实现原型继承: 3、原型继承的特点:

  • 构造函数原型的继承方式分析

    1.通过原型链继承 综上我们可以总结出 通过原型链来实现继承的原理通过原型链来实现继承的原理原型链继承方案中,父类...

  • 2018-06-01 js模拟继承

    这篇主要讲了 构造函数实现继承 原型链实现继承 构造函数+原型链实现继承(两种) 1: call() apply(...

  • 达摩碎碎念--继承

    ECMAScript中只支持实现继承,而实现继承主要是依靠原型链来实现的。 1.原型链 原型链的主要思想是利用原型...

  • 继承方法

    构造函数/原型/实例的关系 借助构造函数实现继承 借助原型链实现继承(弥补构造函数实现继承不足) 组合方式(组合原...

  • 【javascript】继承

    javascript只支持实现继承,而且继承主要是依靠原型链来实现的。 原型链 javascript将原型链作为实...

网友评论

      本文标题:js-原型实现继承

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