← Back to TypeScript Course | Chapter 10: Decorators | Lesson 7 of 7

Decorator Composition की जानकारी

Decorator composition का मतलब है एक ही declaration पर कई decorators apply करना। इनका order मायने रखता है क्योंकि decorator expressions top से bottom evaluate होती हैं, जबकि decorators खुद bottom से top apply होते हैं।
Syntax
typescript
@Decorator1
@Decorator2
class ClassName {}

Multiple Decorators Apply करना

आप एक ही class member पर कई decorators stack कर सकते हैं, एक per line, और TypeScript हर एक को sequence में target को progressively wrap या transform करने के लिए apply करता है।

उदाहरण: Applying Multiple Decorators

typescript
function a(target: any, ctx: ClassMethodDecoratorContext) { console.log("a applied"); }
function b(target: any, ctx: ClassMethodDecoratorContext) { console.log("b applied"); }
class Example {
  @a
  @b
  run() {}
}
new Example();

Evaluation and Application Order

Decorator factories लिखे जाने के order में top-to-bottom evaluate होती हैं, लेकिन वे जो decorators return करती हैं वे असल में bottom-to-top apply होते हैं — final behavior predict करने के लिए इस two-phase order को समझना essential है।

उदाहरण: Evaluation and Application Order

typescript
function factory(name: string) {
  console.log("Evaluating factory", name);
  return function (target: any, ctx: ClassMethodDecoratorContext) {
    console.log("Applying decorator", name);
  };
}
class Example {
  @factory("top")
  @factory("bottom")
  run() {}
}
new Example();

Method Wrappers Compose करना

Decorators compose करना जो हर एक एक method को wrap करता है (जैसे एक logging के लिए और एक timing के लिए) आपको independent method behaviors को layer करने देता है इन्हें एक single, harder-to-maintain decorator में merge किए बिना।

उदाहरण: Composing Method Wrappers

typescript
function withLogging(target: any, ctx: ClassMethodDecoratorContext) {
  return function (this: any, ...args: any[]) {
    console.log("log:", String(ctx.name));
    return target.call(this, ...args);
  };
}
function withTiming(target: any, ctx: ClassMethodDecoratorContext) {
  return function (this: any, ...args: any[]) {
    console.log("timing started");
    return target.call(this, ...args);
  };
}
class Service {
  @withLogging
  @withTiming
  run() {
    return "done";
  }
}
new Service().run();

Different Behaviors Combine करना

अलग decorators एक ही class member में unrelated capabilities जोड़ने के लिए combine किए जा सकते हैं — उदाहरण के लिए एक validation decorator को एक logging decorator के साथ pair करना — जब तक हर एक अब भी एक compatible descriptor return करता है।

उदाहरण: Combining Different Behaviors

typescript
function validate(target: any, ctx: ClassMethodDecoratorContext) {
  return function (this: any, ...args: any[]) {
    console.log("validating");
    return target.call(this, ...args);
  };
}
function log(target: any, ctx: ClassMethodDecoratorContext) {
  return function (this: any, ...args: any[]) {
    console.log("logging");
    return target.call(this, ...args);
  };
}
class Form {
  @validate
  @log
  submit() {
    return "submitted";
  }
}
new Form().submit();

Order Changes Results

चूंकि application order bottom-to-top run होता है, एक ही member पर दो decorators reorder करना यह change कर सकता है कि कौन सा दूसरे का modified output पहले देखता है, तो इनका order swap करना genuinely result change कर सकता है।

उदाहरण: Order Changes Results

typescript
function addOne(target: any, ctx: ClassMethodDecoratorContext) {
  return function (this: any) {
    return target.call(this) + 1;
  };
}
function double(target: any, ctx: ClassMethodDecoratorContext) {
  return function (this: any) {
    return target.call(this) * 2;
  };
}
class MathOp {
  @double
  @addOne
  compute() {
    return 5;
  }
}
console.log(new MathOp().compute());
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. यह मानना कि stacked decorators top-to-bottom apply होते हैं, जब वे bottom-to-top apply होते हैं (सिर्फ factories top-to-bottom evaluate होती हैं)।
  2. Decorators लिखना जो एक दूसरे के order पर depend करते हैं यह notice किए बिना कि lines swap करना result change करता है।
  3. दो wrapping decorators रखना जो दोनों गलत तरीके से original method call करते हैं, तो inner वाला skip हो जाता है।
चैप्टर सारांश
  • Decorators @ syntax उपयोग करके classes, methods, properties, और parameters में extra behavior attach करते हैं।
  • Decorator factories decorators return करते हैं तो वे configuration arguments accept कर सकते हैं।
  • एक ही target पर कई decorators compose किए जा सकते हैं, और वे एक defined order में apply होते हैं।
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.