# es6-mixins

> An easy way to add mixin methods and functions to your es6 classes and react components.

Latest version **1.0.2** (published 2015-04-09) · ISC license · 0 weekly downloads

## Install

```sh
npm install es6-mixins
pnpm add es6-mixins
yarn add es6-mixins
bun add es6-mixins
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.2 |
| Published | 2015-04-09 |
| First published | 2015-04-09 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 15 |
| Author | Jeremy Hitchcock |
| Maintainers | fender-guy |
| Keywords | es6, javascript, mixin, mixins, react, class, method |

## Links

- npm: https://www.npmjs.com/package/es6-mixins
- Repository: https://github.com/fender-guy/es6-mixins
- Issues: https://github.com/fender-guy/es6-mixins/issues
- npm.io page: https://npm.io/package/es6-mixins

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 1.0.2 (latest) — 2015-04-09

## README

## ES6 Mixins

An easy way to add mixin methods and functions to your es6 classes and react components.

#### Installation and Usage:

```bash
npm install es6-mixins
```

Import mixins into your project:

```javascript
import mixins from 'es6-mixins';
```

mixins has 3 arguments

```javascript
mixins([a function, an array, or a class], context, options);
```

Only the first 2 arguments are required. 

* The first argument can be either a function, an array (containing classes or functions), or a class. 
* The second argument is the context on which to add these methods, if used in the constructor it should be set as this. 

The simplest example just adds someFunction to yourClass below.

```javascript
class YourClass {
	constructor(){
		mixins(somefunction, this);
	}
}
```

#### Options Object:

The third argument is an options object that can look like this:

```javascript
{	
	"warn": true // defaults to true	
	"mergeDuplicates": true // defaults to true
}
```

* **warn** If set to true (default) it will warn you when there are two conflicting methods that aren't [react lifecycle methods](https://facebook.github.io/react/docs/component-specs.html).
* **mergeDuplicates** If set to true (default), this will merge two conflicting methods and call the second one first.


## Further Examples:

**Mixing in multiple classes:**

```javascript
import mixins from 'es6-mixins';

// The first class to be used as a mixin
class TestMixin1 {
	testMethod1(){
    	console.log('test Method 1 from TestMixin1');
    }
    
    testMethod2(){
    	console.log('test Method 2 from TestMixin2');
    }
}

// The second class to be used as a mixin
class TestMixin2 {
	testMethod1(){
    	console.log('test Method 1 from TestMixin2');
    }
}

class MainClass {
	constructor(){
    	mixins([TestMixin1, TestMixin2], this);
        testMethod1() // outputs 'test Method 1 from TestMixin2' and then 'test Method 1 from TestMixin1' will warn in console about duplicate methods.
    }
}
```


**Mixing in a function**:

```javascript
import mixins from 'es6-mixins';

// A function to add as a mixin
function testFunction(){
	console.log('test function');
}

class MainClass {
	constructor(){
    	mixins(testFunction, this);
        testFunction() // outputs 'test function'
    }
}
```

---
_Source: https://npm.io/package/es6-mixins · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
