Skip to content
Advertisement

Javascript functions, multiple prototype inheritance

I have an assignment to implement a diagram using javascript prototypes and constructors. For now I have to implement multiple inheritance using prototypes. I know how to implement single inheritance and I am stuck on inheriting multiple prototypes.

UML Diagram

This question is focusing on WeatherData inheriting Event and DataType objects.

JavaScript

I havent tested the code but I am sure it’s wrong because the second .setPrototypeOf() overwrites the first function, which means that the WeatherData‘s prototype will be DataType.

I have searched the internet and could not find answer for this, maybe because this methodology is obsolete.

Advertisement

Answer

One could give the OP’s code a refactoring try of muti-inheritance glue-code like this …

JavaScript

The above constructor implementation covers the mixin part at instance/object level. The code which aggregates and assigns a prototype compound from two other prototype objects is the closest one can come to multiple inheritance with what is available in JS.

But the above code’s design also is flawed in a way that such a compound prototype does loose any further linkage into any of the possibly available prototype chains of either Event or DataType.

Thus from a modeling perspective it was better if the available code base was provided in a way that one could let WeatherData inherit from DataType whereas a prototype agnostic implementation of Event could be applied additionally as function based mixin.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement