day018: JS如何实现继承?继承一定是好的设计吗?​

第六篇: JS如何实现继承?继承一定是好的设计吗?

第一种: 借助call


   
   
  1. function Parent1(){

  2. this.name = 'parent1';

  3. }

  4. function Child1(){

  5. Parent1.call(this);

  6. this.type = 'child1'

  7. }

  8. console.log(new Child1);

这样写的时候子类虽然能够拿到父类的属性值,但是问题是父类原型对象中一旦存在方法那么子类无法继承。那么引出下面的方法。

第二种: 借助原型链


   
   
  1. function Parent2() {

  2. this.name = 'parent2';

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

  4. }

  5. function Child2() {

  6. this.type = 'child2';

  7. }

  8. Child2.prototype = new Parent2();

  9. console.log(new Child2());

看似没有问题,父类的方法和属性都能够访问,但实际上有一个潜在的不足。举个例子:


   
   
  1. var s1 = new Child2();

  2. var s2 = new Child2();

  3. s1.play.push(4);

  4. console.log(s1.play, s2.play);

可以看到控制台:

明明我只改变了s1的play属性,为什么s2也跟着变了呢?很简单,因为两个实例使用的是同一个原型对象。

那么还有更好的方式么?

第三种:将前两种组合


   
   
  1. function Parent3 () {

  2. this.name = 'parent3';

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

  4. }

  5. function Child3() {

  6. Parent3.call(this);

  7. this.type = 'child3';

  8. }

  9. Child3.prototype = new Parent3();

  10. var s3 = new Child3();

  11. var s4 = new Child3();

  12. s3.play.push(4);

  13. console.log(s3.play, s4.play);

可以看到控制台:

之前的问题都得以解决。但是这里又徒增了一个新问题,那就是Parent3的构造函数会多执行了一次(Child3.prototype = new Parent3();)。这是我们不愿看到的。那么如何解决这个问题?

第四种: 组合继承的优化1


   
   
  1. function Parent4 () {

  2. this.name = 'parent4';

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

  4. }

  5. function Child4() {

  6. Parent4.call(this);

  7. this.type = 'child4';

  8. }

  9. Child4.prototype = Parent4.prototype;

这里让将父类原型对象直接给到子类,父类构造函数只执行一次,而且父类属性和方法均能访问,但是我们来测试一下:


   
   
  1. var s3 = new Child4();

  2. var s4 = new Child4();

  3. console.log(s3)

子类实例的构造函数是Parent4,显然这是不对的,应该是Child4。

第五种(最推荐使用): 组合继承的优化1


   
   
  1. function Parent5 () {

  2. this.name = 'parent5';

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

  4. }

  5. function Child5() {

  6. Parent5.call(this);

  7. this.type = 'child5';

  8. }

  9. Child5.prototype = Object.create(Parent5.prototype);

  10. Child5.prototype.constructor = Child5;

这是最推荐的一种方式,接近完美的继承,它的名字也叫做寄生组合继承。

ES6的extends被编译后的JavaScript代码

ES6的代码最后都是要在浏览器上能够跑起来的,这中间就利用了babel这个编译工具,将ES6的代码编译成ES5让一些不支持新语法的浏览器也能运行。

那最后编译成了什么样子呢?


   
   
  1. function _possibleConstructorReturn (self, call) {

  2. // ...

  3. return call && (typeof call === 'object' || typeof call === 'function') ? call : self;

  4. }

  5. function _inherits (subClass, superClass) {

  6. // ...

  7. //看到没有

  8. subClass.prototype = Object.create(superClass && superClass.prototype, {

  9. constructor: {

  10. value: subClass,

  11. enumerable: false,

  12. writable: true,

  13. configurable: true

  14. }

  15. });

  16. if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass;

  17. }

  18. var Parent = function Parent () {

  19. // 验证是否是 Parent 构造出来的 this

  20. _classCallCheck(this, Parent);

  21. };

  22. var Child = (function (_Parent) {

  23. _inherits(Child, _Parent);

  24. function Child () {

  25. _classCallCheck(this, Child);

  26. return _possibleConstructorReturn(this, (Child.__proto__ || Object.getPrototypeOf(Child)).apply(this, arguments));

  27. }

  28. return Child;

  29. }(Parent));

核心是_inherits函数,可以看到它采用的依然也是第五种方式————寄生组合继承方式,同时证明了这种方式的成功。不过这里加了一个Object.setPrototypeOf(subClass, superClass),这是用来干啥的呢?

答案是用来继承父类的静态方法。这也是原来的继承方式疏忽掉的地方。


   
   
  1. 追问: 面向对象的设计一定是好的设计吗?

不一定。从继承的角度说,这一设计是存在巨大隐患的。

从设计思想上谈谈继承本身的问题

假如现在有不同品牌的车,每辆车都有drive、music、addOil这三个方法。


   
   
  1. class Car{

  2. constructor(id) {

  3. this.id = id;

  4. }

  5. drive(){

  6. console.log("wuwuwu!");

  7. }

  8. music(){

  9. console.log("lalala!")

  10. }

  11. addOil(){

  12. console.log("哦哟!")

  13. }

  14. }

  15. class otherCar extends Car{}

现在可以实现车的功能,并且以此去扩展不同的车。

但是问题来了,新能源汽车也是车,但是它并不需要addOil(加油)。

如果让新能源汽车的类继承Car的话,也是有问题的,俗称"大猩猩和香蕉"的问题。大猩猩手里有香蕉,但是我现在明明只需要香蕉,却拿到了一只大猩猩。也就是说加油这个方法,我现在是不需要的,但是由于继承的原因,也给到子类了。

继承的最大问题在于:无法决定继承哪些属性,所有属性都得继承。

当然你可能会说,可以再创建一个父类啊,把加油的方法给去掉,但是这也是有问题的,一方面父类是无法描述所有子类的细节情况的,为了不同的子类特性去增加不同的父类, 代码势必会大量重复,另一方面一旦子类有所变动,父类也要进行相应的更新, 代码的耦合性太高,维护性不好。

那如何来解决继承的诸多问题呢?

用组合,这也是当今编程语法发展的趋势,比如golang完全采用的是面向组合的设计方式。

顾名思义,面向组合就是先设计一系列零件,然后将这些零件进行拼装,来形成不同的实例或者类。


   
   
  1. function drive(){

  2. console.log("wuwuwu!");

  3. }

  4. function music(){

  5. console.log("lalala!")

  6. }

  7. function addOil(){

  8. console.log("哦哟!")

  9. }

  10. let car = compose(drive, music, addOil);

  11. let newEnergyCar = compose(drive, music);

代码干净,复用性也很好。这就是面向组合的设计方式。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值