Course contentsShow
← TypeScript
Lesson 1057 of 1,79624. Classes in TypeScriptFree lesson

Static Methods and this

Understand how static methods work and what this refers to within them (the constructor function).

Static Methods and this

What you'll learn: How this behaves inside static methods and why it refers to the constructor function itself, not an instance.

Understanding this in Static Methods

You already know that static methods belong to the class itself, not to instances. But what does this mean inside a static method?

Here's the key insight: inside a static method, this refers to the constructor function (the class itself), not to any instance.

This is fundamentally different from regular methods, where this points to the instance that called the method.

How It Works

When you use this inside a static method, you're accessing other static members of the class:

class MathHelper {
  static PI = 3.14159;
  
  static calculateCircumference(radius: number): number {
    // 'this' here refers to MathHelper (the class/constructor)
    return 2 * this.PI * radius;
  }
  
  static describePi(): string {
    // 'this' refers to the class, so this.PI works
    return `Pi is approximately ${this.PI}`;
  }
}

console.log(MathHelper.calculateCircumference(10)); // Uses this.PI

You cannot access instance properties from static methods because this doesn't point to any instance—it points to the class constructor.

Think of It This Way

Imagine a blueprint factory. Static methods are like instructions painted on the factory building itself. When those instructions say "use this," they mean "use what's attached to this factory building," not "use what's in any particular product we've made."

Key Takeaway: In static methods, this refers to the class constructor, not an instance. Use it to access other static members, not instance properties.