ES5 继承的方法

来源:2-16 Sub Classes(如何继承一个类?)

诺巴蒂

2019-11-19

感觉 ES5 继承的方式有很多,我觉得您这个是比较简单而且效率相对好的,不好的可能就是 prototype 的 constructor 指向

其他的还有:

  1. 常见的
    Child.prototype = new Father()
    Child.prototype.constructor = Child
    这样不仅多了一次原型链的查找,而且 Child.prototype 还多了很多没用的属性

  2. 好一点的还会通过 Object.cretate 实现
    Child.prototype = Object.create(Father.prototype, {
    constructor: {
    value: Child,
    configurable: true,
    enumerable: false,
    writable: true
    }
    } )
    这样没有了多余的属性,但还是多了一层原型链查找

所以这些方法各有什么好处,我感觉更复杂了

写回答

1回答

快乐动起来呀

2019-11-19

同学不是这样理解的,constructor 也是个对象是引用类型,不存在多了很多属性的

0
1
诺巴蒂
Child.prototype = new Father() 这个会让 Child.prototype 多了 Father 的实例属性, 不是 constructor
2019-11-19
共1条回复

再学JavaScript ES(6-10)全版本语法大全

前端无门槛学习,从ES6到ES10,一套课程掌握JS最新语法

1182 学习 · 336 问题

查看课程