← Back to TypeScript Course | Chapter 22: Mixins | Lesson 2 of 4

Mixins लगाना

एक mixin apply करने का मतलब है एक base class को एक mixin function से pass करना और return हुई class उपयोग करना। कई capabilities वाली एक class बनाने के लिए कई mixins chain किए जा सकते हैं।
Syntax
typescript
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

typescript
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

typescript
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

typescript
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

typescript
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

typescript
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();
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. Mixins को एक अलग order में apply करना और यह देख कर surprised होना कि later वाले same-name methods को earlier वालों पर override कर देते हैं।
  2. Base class pass किए बिना mixin function call करना, जैसे Flyable()।
  3. extends A, B से कई mixins apply करने की कोशिश करना, जो JavaScript में allowed नहीं है।
🔒

Chapter Quiz — Complete all 4 topics to unlock

0/4 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.