Search⌘ K
AI Features

Defining Class Members

Explore how to define and use static properties, static methods, and property getters within JavaScript classes. Understand the dynamic scope of this keyword in static contexts and learn the proper way to call static members using the class name. This lesson helps you grasp the difference between instance and class-level members for better object-oriented design in JavaScript.

When creating abstractions, we often arrive at methods that may not be specific to any particular instance. Sometimes, they are general, and sometimes, they may deal with multiple instances. A class method, instead of an instance method, is used to design these.

Static properties and static methods

JavaScript makes it easy to define static methods and static properties. It does not, however, provide an elegant way to define static fields. Let’s continue with our Car class to add a few static members.

First, let’s add a static field to the class. For this, we have to step ...