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

The Decorator Function Signature

Understanding the arguments a decorator receives: value, context object with kind, name, and access metadata.

The Decorator Function Signature

You'll learn: What arguments a decorator function receives and how to use them to inspect and modify class elements.

When you write a decorator function, JavaScript passes it two crucial arguments that give you everything you need to work with the class element being decorated. Think of these arguments as a detailed instruction manual and toolbox — they tell you what you're decorating and give you the tools to modify it.

The Two Arguments

A decorator receives:

  1. value — The actual thing being decorated (a method, field initializer, getter, setter, or the class itself)
  2. context — An object containing metadata about what's being decorated

The Context Object

The context object is where the magic happens. It contains:

  • kind — A string telling you what is being decorated: "class", "method", "getter", "setter", "field", or "accessor"
  • name — The name of the class member (e.g., "greet" for a method named greet)
  • access — An object with get and/or set methods for reading/writing the decorated element (not available for all kinds)

Additional properties like static and private indicate whether the member is static or private.

function logDecorator(value, context) {
  console.log('Decorating:', context.kind);
  console.log('Name:', context.name);
  console.log('Is static?', context.static);
  
  // Return value unchanged or return a replacement
  return value;
}

The decorator function signature is your interface for introspection — you examine context to understand what you're working with, then decide how to enhance or replace value.

Key Takeaway: Decorators receive two arguments: value (the element being decorated) and context (metadata like kind, name, and access), giving you full introspection capabilities.