【发布时间】:2020-04-12 22:12:18
【问题描述】:
我们如何动态/以编程方式扩展 javascript 类?
更具体地说,给定类似
class Polygon {
constructor(area, sides) {
this.area = area;
this.sides = sides;
}
}
const Rectangle = extend(Polygon, (length, width) => {
super(length * width, 4);
this.length = length;
this.width = width;
});
我们如何实现类似extend 的东西,使其行为与
class Rectangle extends Polygon {
constructor(length, width) {
super(length * width, 4);
this.length = length;
this.width = width;
}
}
?
【问题讨论】:
-
您正在寻找的称为原型链:medium.com/developers-arena/…
标签: javascript class inheritance prototypal-inheritance