InterviewHubPRO
Section Overview
Q355

Question 355 of 521

Part 6: Async Internals, Memory, Microtasks & Web APIs (329 - 403)

68%
JavaScript Internals (521 Qs)•Part 6: Async Internals, Memory, Microtasks & Web APIs (329 - 403)

Does javascript uses mixins

JavaScript does not have built-in support for mixins as a formal language feature. However, developers commonly implement mixins using various patterns to enable code reuse and composition.

A mixin is a way to add reusable functionality from one or more objects into a class or another object, without using classical inheritance. It promotes object composition by combining behaviors or properties from different sources into a single destination.

Practical Code Example

javascript
// JavaScript uses Mixins via Object composition:
const sayHiMixin = {
  sayHi() { console.log(`Hi ${this.name}`); }
};

class User {
  constructor(name) { this.name = name; }
}

Object.assign(User.prototype, sayHiMixin); // Mix in functionality
new User('Alex').sayHi(); // 'Hi Alex'