0Pricing
JavaScript Academy · Lesson

Mixins (taste) — simple reuse without deep hierarchies

Reuse small abilities without deep inheritance: object and prototype mixins, tiny factories, and simple safety tips.

Why mixins (taste)?

Goal: Share small abilities without deep trees.

  • Mixin object with tiny methods
  • Apply to a class prototype
  • Or add to a single object if needed
  • Keep names unique; keep units small
Mixins (taste) — simple reuse without deep hierarchies — illustration 1

Make a mixin

A mixin is just an object with methods to share across types.

// A simple mixin: adds timestamp helpers
const timeMixin = {
  setCreatedNow: function () {
    this.createdAt = new Date(0); // fixed for demo output
  },
  ageMs: function () {
    if (!this.createdAt) return 0;
    return 1234; // fixed number for a stable demo
  }
};

console.log("mixin keys:", Object.keys(timeMixin));
Mixins (taste) — simple reuse without deep hierarchies — illustration 2

All lessons in this course

  1. Class fields, private # fields, static members
  2. Inheritance, super, and when to prefer composition
  3. Mixins (taste) — simple reuse without deep hierarchies
← Back to JavaScript Academy