Decorator Composition की जानकारी
In this page:
@Decorator1
@Decorator2
class ClassName {}
Multiple Decorators Apply करना
आप एक ही class member पर कई decorators stack कर सकते हैं, एक per line, और TypeScript हर एक को sequence में target को progressively wrap या transform करने के लिए apply करता है।
उदाहरण: Applying Multiple Decorators
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
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
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
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
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());
- यह मानना कि stacked decorators top-to-bottom apply होते हैं, जब वे bottom-to-top apply होते हैं (सिर्फ factories top-to-bottom evaluate होती हैं)।
- Decorators लिखना जो एक दूसरे के order पर depend करते हैं यह notice किए बिना कि lines swap करना result change करता है।
- दो 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: