Hello! Welcome to the third lesson in our module on Advanced OOP.
In our last two lessons, we established a solid foundation for JavaScript classes. We saw that class syntax is a cleaner abstraction over prototypes and mastered how to build inheritance hierarchies using extends and super(). A key point was that all the properties and methods we defined belonged to instances of a class—you had to create an object with new to use them.
This brings up a natural question: what if we need functionality or data that is associated with the class as a whole, rather than any single instance? For example, a utility function to compare two instances, a factory method to create pre-configured objects, or a shared constant.
Today, we will address this by learning how to define static methods and properties that belong to the class itself, not to its instances.
Class Members vs. Instance Members
Before we dive into the syntax, it's crucial to understand the conceptual difference between instance members and class (or static) members.
- Instance Members: These belong to a specific object created from a class. Each instance has its own set of instance properties (like
nameorage), and they all share instance methods defined on the class's prototype. - Static Members: These belong directly to the class constructor function itself. They exist in a single location and are not duplicated for each instance. They are ideal for helper functions, factory methods, or data that is constant across all instances of the class.
This diagram provides a clear visual model of the distinction.

Technically, when you define a method or property with the static keyword, you are adding it directly as a property of the class's constructor function, not to ClassName.prototype. This is why it's not available on individual instances, which inherit from the prototype.
Static Methods: Utilities and Factories
Static methods are declared using the static keyword. They are typically used for two main purposes: utility functions and factory methods.
1. Utility Methods
These are helper functions that are logically related to the class but don't need access to an instance's specific data via this. A common example is a method that compares two instances of the class.
To see static utility methods in action, let's watch a segment from a tutorial by freeCodeCamp.org. It clearly demonstrates how to create a static method to compare two instances of a class and another to validate potential dimensions.
Watch the segment from 24:29 to 30:07. Pay attention to how the equals method is called on the Square class directly and how it operates on the instances passed to it as arguments, without using this to refer to an instance.
As you saw in the video, a static method like Square.equals(sq1, sq2) is a clean way to organize comparison logic that is specific to the Square class.
2. Factory Methods
Sometimes, a class's constructor is not the only, or best, way to create an instance. You might want to create an object from a different data format (e.g., JSON) or create a pre-configured version. Static "factory" methods are perfect for this.
The javascript.info article provides an excellent explanation of factory methods. These static methods provide alternative ways to construct objects.
Read the part of the article under the main 'Static properties and methods' heading, focusing on the section that introduces the 'factory' method example with Article.createTodays(). This shows how you can encapsulate specific creation logic within the class itself.
In the Article.createTodays() example, the this inside the static method refers to the Article class itself. Therefore, return new this(...) is equivalent to return new Article(...). This is a powerful pattern for creating flexible and descriptive APIs for your classes.
Static Properties: Shared Data and Constants
Just like methods, properties can also be static. These are useful for storing data that should be shared across all instances of a class, such as constants or configuration settings.
The syntax is straightforward:
class Vector3D {
// Static properties to represent standard basis vectors.
static ZERO = new Vector3D(0, 0, 0);
static UP = new Vector3D(0, 1, 0);
static RIGHT = new Vector3D(1, 0, 0);
// Static property for a configuration value.
static PRECISION = 1e-6;
constructor(x, y, z) {
this.x = x;
this.y = y;
this.z = z;
}
// An instance method can access static properties
// via the class name or, more robustly, `this.constructor`.
isZeroVector() {
return this.x < this.constructor.PRECISION &&
this.y < this.constructor.PRECISION &&
this.z < this.constructor.PRECISION;
}
}
// Accessing static properties directly on the class
console.log(Vector3D.UP); // Vector3D { x: 0, y: 1, z: 0 }
console.log(Vector3D.PRECISION); // 1e-6
const myVec = new Vector3D(0, 0, 0);
console.log(myVec.isZeroVector()); // true
// This will fail, as static properties are not on the instance.
// console.log(myVec.UP); // undefined
Test your understanding!
You are building a Color class. You want to add some predefined standard colors (like red, green, blue) that can be accessed without creating a new Color object every time. You also need a utility method that can blend two colors together to produce a new color.
How would you implement Color.RED as a static property and Color.blend(colorA, colorB) as a static method?
class Color {
constructor(r, g, b) {
this.r = r;
this.g = g;
this.b = b;
}
// TODO: Add static property for RED (255, 0, 0)
// TODO: Add static method 'blend(colorA, colorB)'
// For simplicity, the blend can just average the RGB values.
}
Show answer
Here is the completed implementation:
class Color {
constructor(r, g, b) {
this.r = r;
this.g = g;
this.b = b;
}
// 1. Static property for a predefined color.
static RED = new Color(255, 0, 0);
static GREEN = new Color(0, 255, 0);
static BLUE = new Color(0, 0, 255);
// 2. Static method to act as a utility/factory.
// It takes two instances and returns a new one.
static blend(colorA, colorB) {
const r = (colorA.r + colorB.r) / 2;
const g = (colorA.g + colorB.g) / 2;
const b = (colorA.b + colorB.b) / 2;
// 'this' inside a static method refers to the class (Color).
return new this(r, g, b);
}
}
// Accessing the static property
console.log(Color.RED); // Color { r: 255, g: 0, b: 0 }
// Using the static factory/utility method
const purple = Color.blend(Color.RED, Color.BLUE);
console.log(purple); // Color { r: 127.5, g: 0, b: 127.5 }
This demonstrates both key use cases: Color.RED is a shared, constant value, and Color.blend() is a utility function that operates on Color instances to create a new one.
Inheritance of Static Members
An important feature of static members is that they are inherited by subclasses. This allows you to reuse static functionality and also override it if necessary.
static - JavaScript - MDN Web Docs - Mozilla
For a deeper look into the mechanics of static member inheritance, let's consult the MDN documentation. It provides clear examples and explains how this and super behave in a static context.
Read the sections titled 'Using static members in classes', 'Calling static members from another static method', and 'Calling static members from a class constructor and other methods'. This will solidify your understanding of how statics interact with inheritance and the this keyword.
When a class B extends A, the JavaScript engine sets B's internal [[Prototype]] to A. This means if you call a static method on B that it doesn't have, the engine will look for it on A. This is parallel to how instances of B look up methods on A.prototype.
Conclusion
Today we've distinguished between instance and class-level members and learned how to define the latter using the static keyword. This is a fundamental concept in object-oriented design that allows for cleaner, more organized, and more expressive classes.
Key Takeaways:
- The
statickeyword defines methods and properties directly on the class constructor, not on its prototype. - Static members are ideal for utility functions, factory methods, and class-level constants or configuration.
- They must be accessed via the class name (e.g.,
MyClass.myMethod()) and are not available on instances. - Inside a static method,
thisrefers to the class constructor it was called on. - From an instance method, you can access static members via
this.constructor.myStaticProperty. - Static members are inherited and can be overridden by subclasses.
Next Up
So far, all the properties and methods we've defined—both instance and static—have been public. Anyone can access or modify them. In our next lesson, we will address a crucial aspect of object-oriented programming: encapsulation. We will learn how to achieve true encapsulation using private class fields with the # syntax, allowing us to hide implementation details and create more robust and secure classes.