3 ms·
> Do you think this is a good approach? If not, why? There is no real advantage to bundling data and methods together like: `{...data, ...methods}` This just
by Leftium 3y ago
> Do you think this is a good approach? If not, why?
There is no real advantage to bundling data and methods together like: `{...data, ...methods}`
This just uses extra memory because every player must contain its own duplicate copy of the exact same methods.
Instead, methods should be stored outside the player object and require their input `value` to implement an "interface". For example, the input value for the `setHealth()` method must implement the "Health" interface.
TypeScript provides syntax for defining interfaces. I think JSDoc provides a way to implement interfaces with annotations. Otherwise, you could just manually enforce the interface as a convention.
In fact, JavaScript already allows you to attach methods to objects via Object prototypes. [1]
[1]: https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Object_prototypes https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Ob...