【发布时间】:2021-11-14 00:36:14
【问题描述】:
我有一个使用 javascript 原型和构造函数来实现图表的任务。 现在我必须使用原型实现多重继承。我知道如何实现单一继承,但我一直坚持继承多个原型。
本题关注WeatherData继承Event和DataType对象。
import { DataType, Event } from "./../common/EventData.mjs"
export function WeatherData(value, { time, place }, { type, unit, isInternational }) {
Event.call(time, place)
DataType.call(type, unit, isInternational)
this.value = value
}
WeatherData.setPrototypeOf(WeatherData.prototype, Event.prototype)
WeatherData.setPrototypeOf(WeatherData.prototype, DataType.prototype)
WeatherData.prototype.getValue = function () { return this.value }
我还没有测试过代码,但我确信它是错误的,因为第二个.setPrototypeOf() 覆盖了第一个函数,这意味着WeatherData 的原型将是DataType。
我在互联网上搜索过,但找不到答案,可能是因为这种方法已经过时了。
【问题讨论】:
标签: javascript inheritance javascript-objects prototype