In class driven languages like, say, C# — a way of storing utility methods is through a Static Method.
Objects is the constructor.
keys the static method.
const pizzaData = new Object();
Object.keys(pizzaData) // √
pizzaData.keys // X
Largely memory and data size. Not every object needs to recreate the method for each instance. We really only need one source of truth for a utility method like this.
Not all that different from abstracting a handler in React components. Does each input component need to have its own
onChange method instantiated with the component? Or can the same method be passed down from a parent prop.
So a pattern for working with static methods could be:
- If you need a method that can access the data directly on an instance? Store the method with the instance.
- If need something that converts data from an instance, or is largely utilitarian in function, a static method could be a good option.