Course contentsShow
← JavaScript
Lesson 2085 of 2,72343. DecoratorsFree lesson

Static Members and Decorators

Apply decorators to static methods and fields, understanding differences from instance member decoration.

Static Members and Decorators

You'll learn how decorators behave differently when applied to static class members versus instance members.

The Core Idea

Decorators can be applied to static methods, static fields, and static accessors just like they can for instance members. However, there's a crucial difference: static member decorators receive a different context. Instead of operating on instances, they work on the class constructor itself.

When you decorate a static method or field, the decorator's context object will have context.static set to true, and the target of the decoration is the class constructor rather than the prototype. This means static decorators can modify or instrument behavior at the class level, not the instance level.

How It Works

A static method decorator receives the method and a context where:

  • context.static === true
  • context.kind === "method" (or "field", "accessor", etc.)
  • The method belongs to the class constructor, not ClassName.prototype

Static field decorators run once when the class is defined, not once per instance. This makes them useful for class-level configuration, logging class creation, or validating static properties.

The decorator application order is important: decorators execute from innermost to outermost, and static members are decorated before instance members during class definition.

When to Use Static Decorators

Use static decorators to:

  • Log or track static method calls at the class level
  • Validate static configuration values
  • Add metadata to the class constructor itself
  • Instrument factory methods or singleton patterns

Key Takeaway: Static member decorators operate on the class constructor with context.static === true, allowing you to modify class-level behavior separately from instance behavior.