# auto-bind2

> An ES6 class method automatic binder

Latest version **1.0.3** (published 2017-10-16) · MIT license · 0 weekly downloads

## Install

```sh
npm install auto-bind2
pnpm add auto-bind2
yarn add auto-bind2
bun add auto-bind2
```

## 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.3 |
| Published | 2017-10-16 |
| First published | 2017-10-06 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | John Lyon-Smith |
| Maintainers | jlyonsmith |
| Keywords | bind, auto, method, this |

## Links

- npm: https://www.npmjs.com/package/auto-bind2
- Repository: https://github.com/Jamoki/auto-bind2
- Homepage: https://github.com/Jamoki/auto-bind2#readme
- Issues: https://github.com/Jamoki/auto-bind2/issues
- npm.io page: https://npm.io/package/auto-bind2

## Recent versions

- 1.0.3 (latest) — 2017-10-16
- 1.0.2 — 2017-10-06

## README

# ES6 Class Auto Binder

A Javascript ES6 class autoBind() function with a customizable filter and defaults for React.

## Why Build This?

This package exposes an `autoBind` function that calls `bind()` on 'methods' in you ES6 `class` so that `this` always refers to the `class` object instance and not `window` or some other random object.  It avoids the need to call `bind()` multiple times in your constructor, like this:

```Javascript
class Foo {
  constructor() {
    this.onClick = this.onClick.bind()
    this.onDrag = this.onDrag.bind()
    this.onWhatever = this.onWhatever.bind()
    ...
  }
  ...
}
```

Instead you just write:

```Javascript
class Foo {
  constructor() {
    autoBind(this)
  }
  ...
}
```

There are a few `autoBind` functions out there already, and they work great.  However what I really felt was needed was a fully customizable function that used a filter callback so that it would work with whatever naming conventions you used for your callback functions.

## Usage

Install the package with:

```Shell
npm install auto-bind2
```

Import it using:

```Javascript
import autoBind from 'auto-bind2'
// OR
var autoBind = require('auto-bind2')
```

The function takes a `this` pointer and an optional filter method, e.g.:

```Javascript
autoBind(this)

autoBind(this, (funcName) => (funcName.startsWith('on')))

autoBind(this, (funcName) => (['onClick', 'onDrag', 'onEvent'].includes(funcName)))
```

The library includes support for React.  React `Component` methods are already bound, so you can exclude them with:

```Javascript
import { reactAutoBind } from 'auto-bind2'

reactAutoBind(this)
```

or:

```Javascript
import { isReactMethod, autoBind } from 'auto-bind2'

autoBind(this, isReactMethod)
```

## About

The code is written in ES6 Javascript and built to target NodeJS v8, Chrome v60 and uglification.

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