Mixins लगाना
In this page:
class Combined extends MixinA(MixinB(BaseClass)) {
// members
}
Applying One Mixin
एक plain base class से शुरू करें और इसका constructor एक mixin function में pass करें — वापस आने वाला constructor mixin के behavior के साथ enhanced class represent करता है।
उदाहरण: Applying One Mixin
type Constructor<T = {}> = new (...args: any[]) => T;
function Flyable<TBase extends Constructor>(Base: TBase) {
return class extends Base { fly() { return "Flying"; } };
}
class Bird {}
class FlyingBird extends Flyable(Bird) {}
console.log(new FlyingBird().fly());
Applying Multiple Mixins
Mixin calls को nest करना एक ऐसी class बनाता है जो chain में हर mixin द्वारा supplied हर capability carry करती है; order मायने रख सकता है जब भी एक later mixin ऐसे members पर depend करता है जो chain में earlier एक mixin ने पहले ही जोड़ दिए हैं।
उदाहरण: Applying Multiple Mixins
type Constructor<T = {}> = new (...args: any[]) => T;
function Swimmable<TBase extends Constructor>(Base: TBase) {
return class extends Base { swim() { return "Swimming"; } };
}
function Flyable<TBase extends Constructor>(Base: TBase) {
return class extends Base { fly() { return "Flying"; } };
}
class Duck {}
class SuperDuck extends Swimmable(Flyable(Duck)) {}
const duck = new SuperDuck();
console.log(duck.fly(), duck.swim());
Mixin Composition Function
एक छोटा helper function एक साथ कई mixins के application को centralize कर सकता है, nested function calls की एक chain को एक readable, reusable composition step में बदलते हुए।
उदाहरण: Mixin Composition Function
type Constructor<T = {}> = new (...args: any[]) => T;
function Flyable<TBase extends Constructor>(Base: TBase) {
return class extends Base { fly() { return "flying"; } };
}
function Swimmable<TBase extends Constructor>(Base: TBase) {
return class extends Base { swim() { return "swimming"; } };
}
function compose<TBase extends Constructor>(Base: TBase) {
return Swimmable(Flyable(Base));
}
class Animal {}
const Duck = compose(Animal);
console.log(new Duck().fly(), new Duck().swim());
Mixins with Constructors
एक mixin उस base class को directly extend करके जो constructor arguments base class expect करती है उन्हें preserve कर सकता है — mixin को खुद एक fixed constructor signature नहीं मानना चाहिए जब तक यह explicitly एक को constrain न करे।
उदाहरण: Mixins with Constructors
type Constructor<T = {}> = new (...args: any[]) => T;
function Named<TBase extends Constructor>(Base: TBase) {
return class extends Base {
constructor(...args: any[]) {
super(...args);
}
};
}
class Person {
constructor(public name: string) {}
}
class NamedPerson extends Named(Person) {}
console.log(new NamedPerson("Ravi").name);
Mixin Usage Patterns
Individual mixin functions को छोटा और एक capability पर focused रखें। जब एक particular combination repeatedly apply हो तो एक named, reusable enhanced constructor बनाएं, या एक one-off class के लिए mixins को सिर्फ inline apply करें।
उदाहरण: Mixin Usage Patterns
type Constructor<T = {}> = new (...args: any[]) => T;
function Loggable<TBase extends Constructor>(Base: TBase) {
return class extends Base { log() { console.log("logging"); } };
}
class Service {}
// Applied inline for a one-off class:
class OneOffService extends Loggable(Service) {}
new OneOffService().log();
- Mixins को एक अलग order में apply करना और यह देख कर surprised होना कि later वाले same-name methods को earlier वालों पर override कर देते हैं।
- Base class pass किए बिना mixin function call करना, जैसे
Flyable()। extends A, Bसे कई mixins apply करने की कोशिश करना, जो JavaScript में allowed नहीं है।
Chapter Quiz — Complete all 4 topics to unlock
0/4 topics done
Complete these topics first: