tc39 / proposal-mixins

A template for ECMAScript proposals
MIT License
61 stars 2 forks source link

Constructor? #16

Open FrameMuse opened 2 months ago

FrameMuse commented 2 months ago

I have concerns about constructors in mixins. Allowing declaring constructor in mixins means allowing access to super since it's a must to call super in constructor, which would lead to origin class reference leakage.

As I understand mixins, they adds own properties and methods and encapsulate logic acting like a plain JS object. But allowing a constructor somewhat equals them to classes, which is not that simple.

For initiating logic I would expect to have a dedicated keyword like init

mixin Network {
    online = false

    connect() {...}
    disconnect() {...}

    init {
        this.connect()
    }
}

In case of mixin extension, the extensions would just add up to this as they are a part of origin mixin since there could be multiple mixin extensions.

mixin Events {
    listeners = new Set

    dispatch() {...}
    on() {...}
}

mixin Socket extends Network {
    init {
        this.dispatch(...) // no access to `super` in mixins.
    }
}
FrameMuse commented 2 months ago

Just seen that it repeats the problem from https://github.com/justinfagnani/proposal-mixins/issues/13