# auto-bind

> Automatically bind methods to their class instance

Latest version **5.0.1** (published 2021-10-18) · MIT license · 0 weekly downloads

## Install

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

## Health

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

Positive: has types package; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 5.0.1 |
| Published | 2021-10-18 |
| First published | 2016-08-09 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | separate (@types/auto-bind) |
| Module format | ESM |
| Node | ^12.20.0 \|\| ^14.13.1 \|\| >=16.0.0 |
| Dependencies | 0 |
| Unpacked size | 6.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 461 |
| Author | Sindre Sorhus |
| Maintainers | sindresorhus |
| Keywords | auto, bind, class, methods, method, automatically, prototype, instance, function, this, self, react, component |

## Links

- npm: https://www.npmjs.com/package/auto-bind
- Repository: https://github.com/sindresorhus/auto-bind
- Homepage: https://github.com/sindresorhus/auto-bind#readme
- Issues: https://github.com/sindresorhus/auto-bind/issues
- Funding: https://github.com/sponsors/sindresorhus
- npm.io page: https://npm.io/package/auto-bind

## 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

- 5.0.1 (latest) — 2021-10-18
- 5.0.0 — 2021-10-13
- 4.0.0 — 2019-12-16
- 3.0.0 — 2019-10-19
- 2.1.1 — 2019-10-03
- 2.1.0 — 2019-04-07
- 2.0.0 — 2018-12-23
- 1.2.1 — 2018-06-20
- 1.2.0 — 2018-01-19
- 1.1.0 — 2017-01-30
- 1.0.0 — 2017-01-13
- 0.1.0 — 2016-08-09

## README

# auto-bind

> Automatically bind methods to their class instance

It also correctly binds inherited properties.

## Install

```sh
npm install auto-bind
```

## Usage

```js
import autoBind from 'auto-bind';

class Unicorn {
	constructor(name) {
		this.name = name;
		autoBind(this);
	}

	message() {
		return `${this.name} is awesome!`;
	}
}

const unicorn = new Unicorn('Rainbow');

// Grab the method off the class instance
const message = unicorn.message;

// Still bound to the class instance
message();
//=> 'Rainbow is awesome!'

// Without `autoBind(this)`, the above would have resulted in
message();
//=> Error: Cannot read property 'name' of undefined
```

## API

### autoBind(self, options?)

Bind methods in `self` to their class instance.

Returns the `self` object.

#### self

Type: `object`

An object with methods to bind.

#### options

Type: `object`

##### include

Type: `Array<string | RegExp>`

Bind only the given methods.

##### exclude

Type: `Array<string | RegExp>`

Bind methods except for the given methods.

### React

Same as `autoBind` but excludes the default [React component methods](https://reactjs.org/docs/react-component.html).

```js
import autoBindReact from 'auto-bind/react';

class Foo extends React.Component {
	constructor(props) {
		super(props);
		autoBindReact(this);
	}

	// …
}
```

## Related

- [bind-methods](https://github.com/sindresorhus/bind-methods) - Bind all methods in an object to itself or a specified context

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