Decorator Context Object
You'll learn how the context parameter passed to decorators provides crucial metadata about what's being decorated.
What Is the Context Object?
Every decorator function receives two parameters: the value being decorated and a context object. This context object is a rich metadata bundle that tells your decorator exactly what it's decorating and provides tools to hook into the initialization process.
Think of the context object like an ID badge that comes with every decorated element — it tells you the name, what kind of thing it is, and gives you special access to influence its setup.
Key Properties of Context
The context object contains:
context.kind— A string indicating the decorator target:"class","method","field","getter","setter", or"accessor"context.name— The name of the decorated element (a string or symbol)context.static— Boolean indicating if it's a static membercontext.private— Boolean indicating if it's a private membercontext.addInitializer(fn)— A method to register initialization callbacks
function logDecorator(value, context) {
console.log(`Decorating ${context.kind} named "${String(context.name)}"`);
console.log(`Static: ${context.static}, Private: ${context.private}`);
context.addInitializer(function() {
console.log(`Initialized ${context.name}`);
});
return value;
}
The addInitializer method is particularly powerful — it lets you run code right after the class or instance is constructed, perfect for setup logic that depends on the decorated element.
Key Takeaway: The decorator context object provides essential metadata (kind, name, static, private) and the addInitializer hook, allowing decorators to understand exactly what they're modifying and when to run initialization code.