bind-decorator
Context method binding decorator.
@bind
is just a little faster version of @autobind
for decorating methods only, by binding them to the current context. It is written in TypeScript and follows the latest decorator
s proposal.
- It will
throw
exceptions if decorating anything other thanfunction
; - Since the implementation follows the latest
decorator
s proposal where compartion betweeenthis
andtarget
can not be trusted,@bind
will alwaysreturn
aconfigurable
,get accessor propertyDescriptor
which will memomize the result ofdescriptor.value.bind(this)
by re-defining the property descriptor of the method beeing decorated (Credits goes to autobind-decorator for memoizing the result).
If you are looking for not just method decorator but rather full class bounding decorator check @autobind
.
Install
Install with npm:
$ npm install bind-decorator
Usage
In JavaScript
; static what = 'static'; @bind static { console; } { thiswhat = what; } @bind { console; } const tester = 'bind';const test = tester;tester; // warns 'bind'.; // warns 'bind'.Test; // logs 'static'.
In TypeScript
; ;;tester.test; // warns 'bind'.test; // warns 'bind'.Test.test; // logs 'static'.
Testing
-
npm install
-
npm test
Contributing
-
npm install
-
Make changes
-
If necessary add some tests to
__tests__
-
npm test
-
Make a Pull Request