Lesson Sunday

All of the custom methods we've written up to this point have been instance methods. In this lesson, we'll learn about a different kind of method called a static method (also known as a class method in many languages) which can be called directly on a class.

First, let's take a look at an example of an instance method:

const car = new Car();

We have to create an instance of car first before we can drive it. Because the method is called on a single instance of a car (and not all cars), it is an instance method.

A static method, on the other hand, is called on the class itself. What if we wanted to sort a factory full of cars by color? Well, we can't call that on one car. We need to call it on all of the cars, which means we'd need a static method. We can define a static method in a class like this:

class Car {

  static sort(color) {
    // Code for method here.

As we can see, all we need to do is add the static keyword.

Then, when we want to call the method, we can call it directly on the class:


C#/.NET and Ruby/Rails students will learn a lot more about instance versus static/class methods in their backend courses.

But what do static methods have to do with the code we are writing to make API calls? Well, it would be nice to encapsulate our API logic in its own ES6 class. Encapsulation is the process of storing information inside a class to keep it separate from other logic in our application. It helps us keep our code more modular and organized. That means we can make our code better by creating a class to hold our API logic. However, it doesn't make much sense to have an instance of the class before we make our API call. It's not just extra code - there's really no need to create an instance instead of just calling our method on the class itself. This will become apparent when we do our refactor in the next lesson.

By the way, you've probably already worked with static methods, even if you haven't realized it. For instance, whenever you call a method on the Math class, you are using a static method. In fact, the code we used to generate a random number a few lessons ago uses two static methods: Math.random() and Math.floor().

Math.floor(Math.random() * Math.floor(2));

We also briefly discussed the Promise.all() method a few lessons ago - this is also a static method. A key thing to note about documentation regarding static methods - they don't use the prototype keyword so it would be incorrect to say Promise.prototype.all(). Static methods aren't automatically inherited, unlike prototypal methods. (We can use the extends keyword to ensure that static methods are inherited but that's beyond the scope of this lesson.)

We can also have static properties as well. That just means that we attach properties to the class itself, not an instance of the class. For instance, we could do something like this:

Car.colors = ["red", "green", "blue"];

The class itself now has a colors property with a list of all the colors a car might be.

You probably won't need to use static properties in any of your projects (and they aren't required for any independent projects), but it's still good to know they exist. You will, however, be required to use a static method for this section's independent project.

Lesson 17 of 26
Last updated October 12, 2021