浅谈JavaScript的几种继承实现方式

当前需求: 实现 Student 继承自 Person

如果手动实现继承效果, Person和Student分别写自己的属性和方法, 两个构造函数之间没有任何关联

  • 代码编写繁琐
  • 可维护性低

构造函数Person

  1. function Person(name, age, height, address) {
  2.      this.age = age
  3.      this.height = height
  4.      this.address = address
  5. }
  6.  
  7. Person.prototype.running = function() {
  8.      console.log(“running~”)
  9. }
  10.  
  11. Person.prototype.eating = function() {
  12.      console.log(“eating~”)
  13. }

构造函数Student

  1. function Student(name, age, height, address, sno, score) {
  2.      this.age = age
  3.      this.height = height
  4.      this.address = address
  5.      this.sno = sno
  6.      this.score = score
  7. }
  8.  
  9. Student.prototype.running = function() {
  10.      console.log(“running~”)
  11. }
  12. Student.prototype.eating = function() {
  13.      console.log(“eating~”)
  14. }
  15. Student.prototype.studying = function() {
  16.      console.log(“studying~”)
  17. }

内存图

-1

希望满足的条件功能

Student构造函数满足以下条件

  • 能够重写继承的方法, 但不修改Person原型上的方法.
  • 能够增加方法, 但不会影响Person原型上的方法.

Student构建出的实例对象满足以下条件

  • 有name, age, height, address属性, 并且扩充sno和score. 作为自己独立的属性.
  • 继承running, eating方法, 和Person实例对象的方法有相同的引用.

利用原形链实现方法的继承

方式1: 子类原型指向父类原型

  1. function Student(age, height, address, sno, score) {
  2.      this.age = age
  3.      this.height = height
  4.      this.address = address
  5.      this.sno = sno
  6.      this.score = score
  7. }
  8.  
  9. + Stuednt.prototype = Person.prototype

内存图

-2

缺点

父类和子类共享通一个原型对象, 修改了任意一个, 另外一个也被修改

方式2 子类原型指向父类实例对象

  1. function Student(sno, score) {
  2.      this.sno = sno
  3.      this.score = score
  4. }
  5. + var p = new Person()
  6. + Student.prototype = p

内存图

-3

缺点

  • 属性放在了原型上, 无法通过打印查看.
  • 创建的多个实例对象, 继承的属性不互相独立, 一个实例对象修改属性影响其他的实例对象
  • 要new一个实例, 怪怪怪怪怪怪

借用构造函数继承

方式3 组合继承

  1. function Person(name, age, height, address) {
  2.      this.name = name
  3.      this.ag= age
  4.      this.height = height
  5.      this.address = address
  6. }
  7. Person.prototype.running = 
  8.  
  9. function Student(age, height, address, sno, score) {
  10. + Person.call(this, age, height, address)
  11.      this.sno = sno
  12.      this.score = score
  13. }
  14.  
  15. Student.prototype = new Person()

内存图

-4

优点

解决之前的硬性问题, 实例对象属性独立, 属性放在对象内而不是原型上.

缺点

  • 调用两次父类的构造方法, 性能浪费
    • Student.prototype = new Person()第一次
    • Person.call(this)第二次
  • 调用两次构造方法, 导致子类创建的实例对象上, 保留了两份父类的属性
    • 一份在实例对象的__proto__上, new时产生的
    • 一份在实例对象上, 通过借用构造方法call得到

寄生式继承

思路

属性的继承已经解决, 通过Person.call(this) 解决.

方法的继承未解决, 需要找到 Student.prototype = new Person() 的替代方案

思路1

  1. var obj = {}
  2. obj.__proto__ = Person.prototype
  3. Student.prototype = obj
  4.  
  5. // __proto__为浏览器增加的属性, 解决浏览器兼容性问题可以改为
  6. var obj = {}
  7. Object.setPrototypeOf(obj, Person.prototype)
  8. Student.prototype = obj

思路2

兼容所有浏览器 解决老版本浏览器不支持setPrototypeOf

  1. function F() {}
  2. F.prototype = Person.prototype
  3. Student.prototype = new F()

思路3

Object.create() 传入一个对象作为参数, 并返回一个对象, 返回的对象的原型为传入对象

  1. var obj = Object.create(Person.prototype)
  2. Student.prototype = obj

最终 方式4: 寄生组合式继承

  1. // 工具函数
  2. // 创建对象的过程
  3. function createObject(proto) {
  4.      function F() {}
  5.      F.prototype = proto
  6.      return new F()
  7. }
  8.  
  9. // 将Subtype和Supertype联系在一起
  10. // 寄生式函数
  11. function inherit(Subtype, Supertype) {
  12.      Subtype.prototype = createObject(Supertype.prototype)
  13.      Object.defineProperty(Subtype.prototype, “constructor”, {
  14.          enumerable: false,
  15.          configurable: true,
  16.          writable: true,
  17.          value: Subtype
  18.      })
  19. }
  20.  
  21. function Student(age, height, sno, score) {
  22.      Person.call(this, age, height)
  23.      this.sno = sno
  24.      this.score = score
  25. }
  26.  
  27. + inherit(Student, Person)
  28.  
  29. // 使用方法
  30. Student.prototype.studying = function() {
  31.      console.log(“studying”)
  32. }

使用Person.call实现属性的继承

使用inherit实现方法的继承

  • createObject 使Student.prototype指向Person的prototype, 但中间多一个构造函数F(), 解决方式1 的问题
  • Object.defineProperty 实现 Student.prototype 的 constructor 属性指回Student构造函数.内存图

-1

附: 扩充createObject

最初的设计思想, 是为了实现对象的继承, 所以有了以下的代码

createObject只能够做到构造一个有原型的空对象, 现在想要让构造的对象也有属性

  1. createInfo(proto, age, height) {
  2.      const newObj = this.createObject(proto)
  3.      newObj.age = age
  4.      newObj.height = height
  5.      return newObj
  6. }

到此这篇关于浅谈JavaScript的几种继承实现方式的文章就介绍到这了,更多相关JavaScrip 继承内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

标签

发表评论