Package Exports
- bind-decorator
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (bind-decorator) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
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 decorators purposal.
- It will
throwexceptions if decorating anything other thanfunction; - Since the implementation follows the latest
decorators purposal where compartion betweeenthisandtargetcan not be trusted,@bindwill alwaysreturnaconfigurable,get accessor propertyDescriptorwhich 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
Usage
In 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
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
npm installnpm test
Contributing
npm installnpm run typingsMake changes
If necessary add some tests to
__tests__npm testMake a Pull Request
