Skip to content
静心静心
HOME
DoubtfulCases
github icon
  • JAVASCRIPT

    • 封装Ajax
      • 面试题
        • 模块化JQuery
          • 深拷贝和浅拷贝
            • 有趣的javascript
              • 原型链
                • FAjax copy
                  • Javascrpt 中 方法
                    • JS获取数组的最后一项
                      • js正则正方向查找
                        • js重要
                          • 数字滚动
                            • 纯css手风琴
                              • JS设置全局变量
                                • JS 寄生式组合继承
                                  • 1.原型链继承(使用Call | Bind | apply)
                                    • 2.寄生式组合继承(就是搞个中间类)
                                      • 2.1 优化版本
                                        • 3.利用Object.create来完成寄生组合继承

                                      JS 寄生式组合继承

                                      author iconYYtimer icon大约 1 分钟

                                      此页内容
                                      • 1.原型链继承(使用Call | Bind | apply)
                                      • 2.寄生式组合继承(就是搞个中间类)
                                      • 2.1 优化版本
                                      • 3.利用Object.create来完成寄生组合继承

                                      # JS 寄生式组合继承

                                      # 1.原型链继承(使用Call | Bind | apply)

                                      // 通过call 方法传递参数
                                      function Father() {
                                          console.log("进入父亲的方法i")
                                          this.myFather = "父亲"
                                      }
                                      
                                      Father.prototype.eat = function () {
                                          console.log(this)
                                          console.log(`${this.name}和${this.myFather}一起玩耍}`)
                                      }
                                      
                                      function Son(name, age) {
                                          this.name = name;
                                          this.age = age
                                          Father.call(this)
                                      }
                                      Son.prototype = new Father();
                                      Son.prototype.constructor = Son;
                                      let SonObj = new Son("儿子", 15);
                                      SonObj.eat();
                                      
                                      1
                                      2
                                      3
                                      4
                                      5
                                      6
                                      7
                                      8
                                      9
                                      10
                                      11
                                      12
                                      13
                                      14
                                      15
                                      16
                                      17
                                      18
                                      19
                                      20

                                      # 2.寄生式组合继承(就是搞个中间类)

                                      // 寄生式组合继承 利用中间类来搞
                                      
                                      function Father() {
                                          console.log("进入父亲的方法i")
                                          this.myFather = "父亲"
                                      }
                                      
                                      Father.prototype.eat = function () {
                                          console.log(this)
                                          console.log(`${this.name}和${this.myFather}一起玩耍}`)
                                      }
                                      
                                      function Son(name, age) {
                                          this.name = name;
                                          this.age = age
                                          Father.call(this)
                                      }
                                      
                                      function Middle() {
                                      
                                      }
                                      
                                      Middle.prototype = Father.prototype;
                                      Son.prototype = new Middle();
                                      Son.prototype.constructor = Son;
                                      let SonObj = new Son("儿子", 15);
                                      SonObj.eat();
                                      
                                      1
                                      2
                                      3
                                      4
                                      5
                                      6
                                      7
                                      8
                                      9
                                      10
                                      11
                                      12
                                      13
                                      14
                                      15
                                      16
                                      17
                                      18
                                      19
                                      20
                                      21
                                      22
                                      23
                                      24
                                      25
                                      26
                                      27

                                      # 2.1 优化版本

                                      // 优化终极版本寄生式组合继承 利用中间类来搞
                                      function Father() {
                                          console.log("进入父亲的方法i")
                                          this.myFather = "父亲"
                                      }
                                      
                                      Father.prototype.eat = function () {
                                          console.log(this)
                                          console.log(`${this.name}和${this.myFather}一起玩耍}`)
                                      }
                                      
                                      function Son(name, age) {
                                          this.name = name;
                                          this.age = age
                                          Father.call(this)
                                      }
                                      
                                      function _extends(parent, son) {
                                          function Middle() {
                                              this.constructor = son
                                              this.count = 23
                                          }
                                          Middle.prototype = parent.prototype;
                                          return new Middle()
                                      }
                                      
                                      Son.prototype = _extends(Father, Son)
                                      let SonObj = new Son("儿子", 15);
                                      let SonObj2 = new Son("儿子2", 15);
                                      
                                      SonObj.eat();
                                      SonObj2.eat();
                                      
                                      1
                                      2
                                      3
                                      4
                                      5
                                      6
                                      7
                                      8
                                      9
                                      10
                                      11
                                      12
                                      13
                                      14
                                      15
                                      16
                                      17
                                      18
                                      19
                                      20
                                      21
                                      22
                                      23
                                      24
                                      25
                                      26
                                      27
                                      28
                                      29
                                      30
                                      31
                                      32

                                      # 3.利用Object.create来完成寄生组合继承

                                      
                                      function Father() {
                                          console.log("进入父亲的方法i")
                                          this.myFather = "父亲"
                                      }
                                      
                                      Father.prototype.eat = function () {
                                          console.log(this)
                                          console.log(`${this.name}和${this.myFather}一起玩耍}`)
                                      }
                                      
                                      function Son(name, age) {
                                          this.name = name;
                                          this.age = age
                                          Father.call(this)
                                      }
                                      
                                      Son.prototype = Object.create(Father.prototype, {
                                          count: {
                                              writable: true,
                                              value: 23
                                          }
                                      })
                                      Son.prototype.constructor = Son
                                      let SonObj = new Son("儿子", 15);
                                      let SonObj2 = new Son("儿子2", 15);
                                      
                                      console.log(SonObj2);
                                      SonObj.eat();
                                      SonObj2.eat();
                                      
                                      1
                                      2
                                      3
                                      4
                                      5
                                      6
                                      7
                                      8
                                      9
                                      10
                                      11
                                      12
                                      13
                                      14
                                      15
                                      16
                                      17
                                      18
                                      19
                                      20
                                      21
                                      22
                                      23
                                      24
                                      25
                                      26
                                      27
                                      28
                                      29
                                      30
                                      edit icon编辑此页open in new window
                                      上一页
                                      JS设置全局变量
                                      傻瓜都能写出计算机可以理解的代码。唯有能写出人类容易理解的代码的,才是优秀的程序员。
                                      Copyright © 2022 YY

                                      该应用可以安装在您的 PC 或移动设备上。这将使该 Web 应用程序外观和行为与其他应用程序相同。它将在出现在应用程序列表中,并可以固定到主屏幕,开始菜单或任务栏。此 Web 应用程序还将能够与其他应用程序和您的操作系统安全地进行交互。

                                      详情