Stacking Multiple Decorators
What you'll learn
How decorators compose when you stack multiple decorators on the same class member, and the crucial bottom-to-top execution order.
How decorator stacking works
When you apply multiple decorators to the same target (method, field, accessor, or class), they execute from bottom to top—the decorator closest to the target runs first, then the one above it, and so on.
Think of it like wrapping a gift multiple times: the innermost layer goes on first, then each outer layer wraps what came before.
function first(target, context) {
console.log("first decorator executed");
return function(...args) {
console.log("first wrapper called");
return target.call(this, ...args);
};
}
function second(target, context) {
console.log("second decorator executed");
return function(...args) {
console.log("second wrapper called");
return target.call(this, ...args);
};
}
class Example {
@second
@first
greet() {
console.log("original method");
}
}
// During class definition:
// "first decorator executed"
// "second decorator executed"
const ex = new Example();
ex.greet();
// "second wrapper called"
// "first wrapper called"
// "original method"
Why bottom-to-top?
This order ensures the decorator nearest the target has first access to modify it. Each decorator receives the result of the decorator below, allowing them to build on each other's transformations. The topmost decorator produces the final wrapped version.
Key Takeaway: Multiple decorators stack from bottom to top—the decorator closest to the target executes first, and each decorator wraps the result of those below it, creating a chain of composed behaviors.