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

Mixin Pattern की जानकारी

एक mixin एक reusable object-oriented pattern है जो एक class-like component से एक दूसरी class में behavior जोड़ता है। TypeScript में, mixins आमतौर पर उन functions से implement किए जाते हैं जो एक base constructor को extend करते हैं।
Syntax
typescript
type Constructor<T = {}> = new (...args: any[]) => T;
function MixinName<TBase extends Constructor>(Base: TBase) {
  return class extends Base {
    // added members
  };
}

What Is a Mixin?

एक mixin multiple inheritance की ज़रूरत के बिना एक class में reusable behavior जोड़ता है — एक mixin function एक constructor को input के रूप में लेता है और एक नया constructor return करता है जिसमें original के सभी members plus added वाले होते हैं।

उदाहरण: What Is a Mixin?

typescript
type Constructor<T = {}> = new (...args: any[]) => T;
function Timestamped<TBase extends Constructor>(Base: TBase) {
  return class extends Base {
    timestamp = Date.now();
  };
}
class Point {}
class TimestampedPoint extends Timestamped(Point) {}
console.log(typeof new TimestampedPoint().timestamp);

Mixin with Methods

एक mixin किसी भी class में एक या ज़्यादा methods जोड़ सकता है जिस पर यह apply किया जाए; mixin function से return हुई class दोनों original class के members और mixin द्वारा contributed नया behavior carry करती है।

उदाहरण: Mixin with Methods

typescript
type Constructor<T = {}> = new (...args: any[]) => T;
function Serializable<TBase extends Constructor>(Base: TBase) {
  return class extends Base {
    serialize() { return JSON.stringify(this); }
  };
}
class Point { x = 1; y = 2; }
class SerializablePoint extends Serializable(Point) {}
console.log(new SerializablePoint().serialize());

Combining Mixins

कई mixins को एक के बाद एक chain किया जा सकता है, हर mixin function chain में पिछले mixin द्वारा पहले से return किए गए constructor को receive करते हुए, capabilities को एक-दूसरे पर layer करते हुए।

उदाहरण: Combining Mixins

typescript
type Constructor<T = {}> = new (...args: any[]) => T;
function Named<TBase extends Constructor>(Base: TBase) {
  return class extends Base { name = "unnamed"; };
}
function Aged<TBase extends Constructor>(Base: TBase) {
  return class extends Base { age = 0; };
}
class Base {}
class Person extends Aged(Named(Base)) {}
const p = new Person();
console.log(p.name, p.age);

Mixin State

Mixins methods के साथ-साथ instance fields भी जोड़ सकते हैं — वे fields returned class से बनाए हर individual instance से belong करते हैं, इसलिए हर instance को उस state की अपनी independent copy मिलती है।

उदाहरण: Mixin State

typescript
type Constructor<T = {}> = new (...args: any[]) => T;
function Counter<TBase extends Constructor>(Base: TBase) {
  return class extends Base {
    count = 0;
    increment() { this.count++; }
  };
}
class Base {}
class Widget extends Counter(Base) {}
const w1 = new Widget();
const w2 = new Widget();
w1.increment();
console.log(w1.count, w2.count);

Mixin Benefits

Mixins तब फ़ायदा देते हैं जब कई otherwise-unrelated classes को exactly same behavior चाहिए हो, एक गहरी, rigid inheritance hierarchy में सब कुछ force करने के बजाय छोटी, composable capabilities को encourage करते हुए।

उदाहरण: Mixin Benefits

typescript
type Constructor<T = {}> = new (...args: any[]) => T;
function Loggable<TBase extends Constructor>(Base: TBase) {
  return class extends Base {
    log(msg: string) { console.log(`[LOG] ${msg}`); }
  };
}
class Service {}
class ApiService extends Loggable(Service) {}
new ApiService().log("Request received");
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. एक mixin लिखना जो Constructor<T> उपयोग नहीं करता, इसलिए class extends Base fail होता है क्योंकि Base को constructable नहीं जाना जाता।
  2. mixin function के खिलाफ instanceof checks की उम्मीद करना, क्योंकि यह एक class नहीं है।
  3. एक mixin को एक ऐसी class के साथ उपयोग करना जिसका constructor (...args: any[]) से अलग arguments लेता है।
🔒

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.