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

诺巴蒂
2019-11-19
感觉 ES5 继承的方式有很多,我觉得您这个是比较简单而且效率相对好的,不好的可能就是 prototype 的 constructor 指向
其他的还有:
-
常见的
Child.prototype = new Father()
Child.prototype.constructor = Child
这样不仅多了一次原型链的查找,而且 Child.prototype 还多了很多没用的属性 -
好一点的还会通过 Object.cretate 实现
Child.prototype = Object.create(Father.prototype, {
constructor: {
value: Child,
configurable: true,
enumerable: false,
writable: true
}
} )
这样没有了多余的属性,但还是多了一层原型链查找
所以这些方法各有什么好处,我感觉更复杂了
写回答
1回答
-
快乐动起来呀
2019-11-19
同学不是这样理解的,constructor 也是个对象是引用类型,不存在多了很多属性的
012019-11-19
相似问题