npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

sirrobert-mixin

v1.3.3

Published

A Mixin module for node with correct inheritance.

Downloads

8

Readme

Rationale

ECMAScript 6 has classes and inheritance (via extends). It should also have mixins (that I like). Now it does.

Installation

Local installation

npm install --save sirrobert-mixin

Global installation

npm install --global sirrobert-mixin

Usage

Writing a Good Mixin

The key to writing a good mixin is to make it a composable function. The idea is that the mixin definition is actually a factory rather than a function itself.

Mixins are Factories (not functions)

Here's the pattern:

let MyMixin = (superclass) => class extends superclass {
  constructor (params) {
    super(params);
    // ... and other stuff
  }
}

When you use the Mixin.with() method (or class method), you are passing in the superclass and it does a little magic inside to create a new inheritance chain. In particular, it looks like this:

// Class definition
class B extends Mixin(A).with(X,Y) { ... }

// Inheritance chain
B > X > Y > A

If you want a chain more like this:

// Mixins are after A
B > A > X > Y

You would use this:

class A extends Mixin.with(X, Y) {}
class B extends A {}

Building some Classes

Let's build some classes and see the inheritance chain in action. First, we load the module and define a base class and some mixins.

// I like to use "Mix" because it's a short verb.
const Mix = require("sirrobert-mixin");

class A {
  constructor () {
    this.foo = "Foo from A";
    this.a = "only in A";
  }
}

let Mixin1 = (superclass) => class extends superclass {
  constructor (params) {
    super(params);
    this.foo = "Foo from Mixin1";
    this.mixin1 = "only in mixin1";
  }
}

let Mixin2 = (superclass) => class extends superclass {
  constructor (params) {
    super(params);
    this.foo = "Foo from Mixin2";
    this.mixin2 = "only in mixin2";
  }
}

// This one doesn't have a .foo property so we can see the transparency.
let MixinNoFoo = (superclass) => class extends superclass {
  constructor (params) {
    super(params);
    this.mixinNoFoo = "only in mixinNoFoo";
  }
}

Now let's see how we can mix and match these into new classes.

Create a class that uses one mixin.

class MyClass extends Mix.with(Mixin1) {
  constructor (params) {
    super(params);
    this.myClass = "only in myClass";
  }
}

console.log(new MyClass().myClass);  // "only in myClass"
console.log(new MyClass().foo);      // "Foo from Mixin1"

Create a class with multiple mixins

With multiple mixins, the earlier mixins in the list override the latter.

class MyClass extends Mix.with(Mixin1, Mixin2) {
  constructor (params) {
    super(params);
    this.myClass = "only in myClass";
  }
}

console.log(new MyClass().myClass);  // "only in myClass"
console.log(new MyClass().foo);      // "Foo from Mixin1"
console.log(new MyClass().mixin1);   // "only in Mixin1"
console.log(new MyClass().mixin2);   // "only in Mixin2"

Create a class with superclass and mixins

When extending another class, you can insert mixins into the chain.

class MyClass extends Mix(A).with(Mixin1, Mixin2) {
  constructor (params) {
    super(params);
    this.myClass = "only in myClass";
  }
}

console.log(new MyClass().myClass);  // "only in myClass"
console.log(new MyClass().foo);      // "Foo from Mixin1"
console.log(new MyClass().mixin1);   // "only in Mixin1"
console.log(new MyClass().mixin2);   // "only in Mixin2"
console.log(new MyClass().a);        // "only in A"

Sugar

The .with() method is available as an instance method and a class method. This allows you to add mixins when you have a base class, but also when you just want the mixins. These are equivalent, but I think the second looks neater, having less syntactic noise.

class B extends Mix().with(X,Y) {}
class B extends Mix.with(X,Y) {}