phx-hook
accepts as argument an object where its methods are the lifecycle of the callbacks (mounted, updated, etc.).
This is awesome, but it would be nice also to accept a constructor function (that would be invoked upon component instantiation), so we could make use Javascript classes. It could easily be integrated with a simple condition: javascript - How to check if a variable is an ES6 class declaration? - Stack Overflow .
let instance;
if (typeof phxHookArgument === 'function'){
// It's a function, so it definitely can't be an instance.
instance = new phxHookArgument();
} else {
// It could be anything other than a constructor. Let's assume it's our regular object with callbacks.
instance = {...phxHookArgument};
}
Using classes would allow composition patterns and better coding re-using.
Now we can use object composition, but it’s a bit limiting (no super()
support, for example).