Chris Padilla/Blog / Tech

Static Methods

In class driven languages like, say, C# — a way of storing utility methods is through a Static Method.

Say you're working in JavaScript. A great ol' example of this is the Object.keys() method.

Objects is the constructor. keys the static method.

(technically not a class since JavaScript only mimics classes. More details on that in You Don't Know JS by Kyle Simpson and contributors.)

In JavaScript, you can access the method on the constructor through dot notation. But when creating an object, that object doesn't have a keys method.

const pizzaData = new Object();

Object.keys(pizzaData) // √

pizzaData.keys // X

The benefits?

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.