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

mixed

v0.4.0

Published

Minimalist, lightweight, ES5-compatible mixins.

Downloads

12

Readme

Synopsis

mixed is a minimalist, lightweight, ES3-compatible function to mix Constructor functions and their prototypes into instance objects.

stability 3 - stable license - Unlicense Flattr this

browser support

Build Status Coverage Status Dependencies

NPM status

Rationale

There are seem to be two common approaches to dealing with mixins:

  1. Mixins are special constructor functions that take an optional instance argument to augment an existing instance rather than the this context. This makes for very nice syntax, but requires the author to support mixing intentionally.

  2. Mixins are just collections of methods. In this case mixing is practically equivalent to the functionality already provided by aug or jQuery's $.extend function.

The first approach (used by many libraries in component) provides a lot of power and flexibility, representing the same functionality mixins provide in class-based languages. However, the overhead of providing a mixin mechanism for each constructor individually seems obviously redundant.

Additionally, modifying the constructor's argument list to allow using it as a mixin seems both intrusive and limitting, as many constructors would normally expect to be passed a configuration object or initial parameters.

mixed tries to solve this issue by both providing a standalone mixin function, to allow mixing any given constructor into any given object, and also providing a thin wrapper interface to turn any plain old argument-free constructor function into a component-style mixin that can be called either as a constructor (with the new keyword) or with an object to mix into (as the sole argument).

Install

Node.js

With NPM

npm install mixed

From source

git clone https://github.com/pluma/mixed.git
cd mixed
npm install
make && make dist

Browser

With component

component install pluma/mixed

Learn more about component.

With bower

bower install mixed

Learn more about bower.

With a CommonJS module loader

Download the latest minified CommonJS release and add it to your project.

Learn more about CommonJS modules.

With an AMD module loader

Download the latest minified AMD release and add it to your project.

Learn more about AMD modules.

As a standalone library

Download the latest minified standalone release and add it to your project.

<script src="/your/js/path/mixed.globals.min.js"></script>

This makes the mixed module available in the global namespace.

Basic mixin usage example

function Liquor() {
    this.alcoholContent = 0.8;
}
Liquor.prototype = {
    burn: function() {
        this.alcoholContent *= 0.5;
        if (this.alcoholContent > 0.2) {
            console.log('*FOOSH*');
        } else {
            console.log('*fizzle*');
        }
    }
};

var cocktail = {};
console.log(cocktail.alcoholContent); // undefined
console.log(cocktail.burn); // undefined

mixed.mixin(Liquor, cocktail);
console.log(cocktail.alcoholContent); // 0.8
cocktail.burn(); // *FOOSH*
console.log(cocktail.alcoholContent); // 0.4

Basic mixable usage example

function Liquor() {
    this.alcoholContent = 0.8;
}
Liquor.prototype = {
    burn: function() {
        this.alcoholContent *= 0.5;
        if (this.alcoholContent > 0.2) {
            console.log('*FOOSH*');
        } else {
            console.log('*fizzle*');
        }
    }
};
Liquor = mixed.mixable(Liquor);

var cocktail = {};
console.log(cocktail.alcoholContent); // undefined
console.log(cocktail.burn); // undefined

Liquor(cocktail);
console.log(cocktail.alcoholContent); // 0.8
cocktail.burn(); // *FOOSH*
console.log(cocktail.alcoholContent); // 0.4

API

mixin(ctor:Function, obj, args…):Object

Applies the given constructor to the given object and returns the object.

Copies each of the constructor's prototype's properties to the given object, then calls the constructor as a function with the object as its context (this).

Any additional arguments will be passed to the constructor function.

NOTE: If you simply want to merge two instances rather than messing with constructors and prototypes, consider using aug instead.

mixable(ctor:Function, args…):Function

Creates a wrapper around the given constructor function that can be called either as a constructor (using the new keyword) or as a mixin (with the object to mix into as the sole argument). The constructor's prototype will be copied over to the wrapper function.

If called as a mixin, this wrapper behaves exactly as if mixin was called directly.

Acknowledgments

This library was influenced by TJ Holowaychuk's work on component.

Unlicense

This is free and unencumbered public domain software. For more information, see http://unlicense.org/ or the accompanying UNLICENSE file.