UNPKG

3.49 kBMarkdownView Raw
1# bind-decorator
2
3Context method binding decorator.
4
5[![npm version](https://badge.fury.io/js/bind-decorator.svg)](https://badge.fury.io/js/bind-decorator)
6[![license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/NoHomey/bind-decorator)
7[![Build Status](https://semaphoreci.com/api/v1/nohomey/bind-decorator/branches/master/badge.svg)](https://semaphoreci.com/nohomey/bind-decorator)
8[![Code Climate](https://codeclimate.com/github/NoHomey/bind-decorator/badges/gpa.svg)](https://codeclimate.com/github/NoHomey/bind-decorator)
9[![Test Coverage](https://codeclimate.com/github/NoHomey/bind-decorator/badges/coverage.svg)](https://codeclimate.com/github/NoHomey/bind-decorator/coverage)
10[![Issue Count](https://codeclimate.com/github/NoHomey/bind-decorator/badges/issue_count.svg)](https://codeclimate.com/github/NoHomey/bind-decorator)
11![TypeScript](https://img.shields.io/badge/%3C%20%2F%3E-TypeScript-blue.svg)
12![Typings](https://img.shields.io/badge/typings-%E2%9C%93-brightgreen.svg)
13
14`@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/).
15
16- It will `throw` exceptions if decorating anything other than `function`;
17- 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).
18
19If 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).
20
21# Install
22
23Install with npm:
24
25```bash
26$ npm install bind-decorator
27```
28
29[![NPM](https://nodei.co/npm/bind-decorator.png?downloads=true&stars=true)](https://nodei.co/npm/bind-decorator/)
30
31# Usage
32
33## In JavaScript
34
35```javascript
36import bind from 'bind-decorator';
37
38class Test {
39 static what = 'static';
40
41 @bind
42 static test() {
43 console.log(this.what);
44 }
45
46 constructor(what) {
47 this.what = what;
48 }
49
50 @bind
51 test() {
52 console.warn(this.what);
53 }
54}
55
56const tester = new Test('bind');
57const { test } = tester;
58tester.test(); // warns 'bind'.
59test(); // warns 'bind'.
60Test.test(); // logs 'static'.
61```
62
63## In TypeScript
64
65```typescript
66import bind from 'bind-decorator';
67
68class Test {
69 public static what: string = 'static';
70
71 @bind
72 public static test(): void {
73 console.log(this.what);
74 }
75
76 public constructor(public what: string) {
77 this.what = what;
78 }
79
80 @bind
81 public test(): void {
82 console.warn(this.what);
83 }
84}
85
86const tester: Test = new Test('bind');
87const { test } = tester;
88tester.test(); // warns 'bind'.
89test(); // warns 'bind'.
90Test.test(); // logs 'static'.
91```
92
93# Testing
94
951. `npm install`
96
972. `npm test`
98
99# Contributing
100
1011. `npm install`
102
1032. Make changes
104
1053. If necessary add some tests to `__tests__`
106
1074. `npm test`
108
1095. Make a Pull Request
\No newline at end of file