T
T
Taras Labiak2014-11-28 11:03:42
JavaScript
Taras Labiak, 2014-11-28 11:03:42

Do I need to implement multiple inheritance?

In my project for inheritance, I implemented the function

function inherit(child, parent, proto, descriptor) {
    if (!descriptor)
        descriptor = {};
    descriptor['base'] = {
        value: parent,
        enumerable: false,
        writable: false
    };
    child.prototype = Object.create(parent.prototype);
    child.prototype.constructor = child;
    var names = proto ? Object.getOwnPropertyNames(proto) : [];
    for (var i in names) {
        var name = names[i];
        descriptor[name] = Object.getOwnPropertyDescriptor(proto, name);
    }
    Object.defineProperties(child.prototype, descriptor);
    child.descriptor = descriptor;
    return child;
}

If in child classes you need to refer to the getters-setters of the base one, then child.descriptor is used.
What would you advise if I want to pass many classes (constructors) as parent?
Or is it better to extend the prototype after applying the inheritance function?
Actually for this I also have a bike:
function ext(target, extension) {
    if (target.prototype)
        target = target.prototype;
    for (var key in extension)
        if (!(key in target))
            Object.defineProperty(target, key, {
                value: extension[key],
                enumerable: false
            });
}

True, it is intended to be a safe extension of base classes, such as HTMLElement

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Konstantin Kitmanov, 2014-11-28
@k12th

Multiple inheritance leads to a lot of the same problems :)
Better attach the mechanism of mixins, and even better - component-oriented programming.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question