# bind-decorator

> The fastest automatic method.bind(this) decorator

Latest version **1.0.11** (published 2017-07-20) · MIT license · 0 weekly downloads

## Install

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

## Health

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

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.11 |
| Published | 2017-07-20 |
| First published | 2016-09-07 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 71 |
| Author | Ivo Stratev |
| Maintainers | nohomey |
| Keywords | bind, autobind, methodbind, bind-decorator, autobind-decorator, decorator |

## Links

- npm: https://www.npmjs.com/package/bind-decorator
- Repository: https://github.com/NoHomey/bind-decorator
- Homepage: https://github.com/NoHomey/bind-decorator#readme
- Issues: https://github.com/NoHomey/bind-decorator/issues
- npm.io page: https://npm.io/package/bind-decorator

## Alternatives

- [localforage](https://npm.io/package/localforage.md) — 6.2M weekly downloads
- [localforage-observable](https://npm.io/package/localforage-observable.md) — 30.8K weekly downloads
- [@y/y](https://npm.io/package/@y/y.md) — 30.1K weekly downloads
- [@metaobjectsdev/render](https://npm.io/package/@metaobjectsdev/render.md) — 3.5K weekly downloads
- [@ledgerhq/coin-algorand](https://npm.io/package/@ledgerhq/coin-algorand.md) — 1.1K weekly downloads

## Recent versions

- 1.0.11 (latest) — 2017-07-20
- 1.0.10 — 2016-10-19
- 1.0.9 — 2016-10-19
- 1.0.8 — 2016-10-18
- 1.0.7 — 2016-09-20
- 1.0.6 — 2016-09-13
- 1.0.5 — 2016-09-12
- 1.0.4 — 2016-09-12
- 1.0.3 — 2016-09-08
- 1.0.2 — 2016-09-08
- 1.0.1 — 2016-09-07
- 1.0.0 — 2016-09-07
- 0.0.1-pre — 2016-09-07

## README

# bind-decorator

Context method binding decorator.

[![npm version](https://badge.fury.io/js/bind-decorator.svg)](https://badge.fury.io/js/bind-decorator)
[![license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/NoHomey/bind-decorator)
[![Build Status](https://semaphoreci.com/api/v1/nohomey/bind-decorator/branches/master/badge.svg)](https://semaphoreci.com/nohomey/bind-decorator)
[![Code Climate](https://codeclimate.com/github/NoHomey/bind-decorator/badges/gpa.svg)](https://codeclimate.com/github/NoHomey/bind-decorator)
[![Test Coverage](https://codeclimate.com/github/NoHomey/bind-decorator/badges/coverage.svg)](https://codeclimate.com/github/NoHomey/bind-decorator/coverage)
[![Issue Count](https://codeclimate.com/github/NoHomey/bind-decorator/badges/issue_count.svg)](https://codeclimate.com/github/NoHomey/bind-decorator)
![TypeScript](https://img.shields.io/badge/%3C%20%2F%3E-TypeScript-blue.svg)
![Typings](https://img.shields.io/badge/typings-%E2%9C%93-brightgreen.svg)

`@bind` is just a little faster version of [`@autobind`](https://github.com/andreypopp/autobind-decorator/blob/master/src/index.js) for decorating methods only, by binding them to the current context. It is written in TypeScript and follows the latest `decorator`s [proposal](http://tc39.github.io/proposal-decorators/).

- It will `throw` exceptions if decorating anything other than `function`;
- Since the implementation follows the latest `decorator`s [proposal](http://tc39.github.io/proposal-decorators/) where compartion betweeen `this` and `target` can not be trusted, `@bind` will always `return` a `configurable`, `get accessor propertyDescriptor` which will memomize the result of `descriptor.value.bind(this)` by re-defining the property descriptor of the method beeing decorated (Credits goes to [autobind-decorator](https://github.com/andreypopp/autobind-decorator/blob/master/src/index.js) for memoizing the result).

If you are looking for not just method decorator but rather full class bounding decorator check [`@autobind`](https://github.com/andreypopp/autobind-decorator/blob/master/src/index.js).

# Install

Install with npm:

```bash
$ npm install bind-decorator
```

[![NPM](https://nodei.co/npm/bind-decorator.png?downloads=true&stars=true)](https://nodei.co/npm/bind-decorator/)

# Usage

## In JavaScript

```javascript
import bind from 'bind-decorator';

class Test {
    static what = 'static';
    
    @bind
    static test() {
        console.log(this.what);
    }

    constructor(what) {
        this.what = what;
    }

    @bind
    test() {
        console.warn(this.what);
    }
}

const tester = new Test('bind');
const { test } = tester;
tester.test(); // warns 'bind'.
test(); // warns 'bind'.
Test.test(); // logs 'static'.
```

## In TypeScript

```typescript
import bind from 'bind-decorator';

class Test {
    public static what: string = 'static';
    
    @bind
    public static test(): void {
        console.log(this.what);
    }

    public constructor(public what: string) {
        this.what = what;
    }

    @bind
    public test(): void {
        console.warn(this.what);
    }
}

const tester: Test = new Test('bind');
const { test } = tester;
tester.test(); // warns 'bind'.
test(); // warns 'bind'.
Test.test(); // logs 'static'.
```

# Testing

1. `npm install`

2. `npm test`

# Contributing

1. `npm install`

2. Make changes

3. If necessary add some tests to `__tests__`

4. `npm test`

5. Make a Pull Request

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