proposal-mixins icon indicating copy to clipboard operation
proposal-mixins copied to clipboard

Constructor?

Open FrameMuse opened this issue 6 months ago • 1 comments

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 avatar Aug 11 '24 11:08 FrameMuse