bind-decorator
Advanced tools
Comparing version 1.0.2 to 1.0.3
@@ -1,2 +0,2 @@ | ||
export declare function bind<T extends Function>(target: Object, propertyKey: string | symbol, descriptor: TypedPropertyDescriptor<T>): TypedPropertyDescriptor<T> | void; | ||
export declare function bind<T extends Function>(target: Object, propertyKey: string, descriptor: TypedPropertyDescriptor<T>): TypedPropertyDescriptor<T> | void; | ||
export default bind; |
@@ -8,3 +8,6 @@ "use strict"; | ||
get: function () { | ||
return descriptor.value.bind(this); | ||
var bound = descriptor.value.bind(this); | ||
// Credits to https://github.com/andreypopp/autobind-decorator for memoizing the result of bind against a symbol on the instance. | ||
Object.defineProperty(this, propertyKey, { value: bound, configurable: true, writable: true }); | ||
return bound; | ||
} | ||
@@ -11,0 +14,0 @@ }; |
{ | ||
"name": "bind-decorator", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "The fastest automatic method.bind(this) decorator", | ||
@@ -5,0 +5,0 @@ "main": "./dist/index.js", |
# bind-decorator | ||
The best automatic context method binding decorator | ||
- It will `throw` exceptions if decorating anything other than `function`, [run this](throws.md); | ||
- Since the implementation follows the latest `decorator`s [purposal](http://tc39.github.io/proposal-decorators/) where compartion betweeen `this` and `target` can not be trusted, [run this if you don't belive me](not-comparable.md). `@bind` will always `return` a `configurable`, `enumerable` `get accessor propertyDescriptor` with value of `descriptor.value.bind(this)`. | ||
Context method binding decorator. | ||
In fact the whole implementation is just 12 lines of code: | ||
`@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 [purposal](http://tc39.github.io/proposal-decorators/). | ||
```typescript | ||
export function bind<T extends Function>(target: Object, propertyKey: string | symbol, descriptor: TypedPropertyDescriptor<T>): TypedPropertyDescriptor<T> | void { | ||
if(!descriptor || (typeof descriptor.value !== 'function')) throw new TypeError(`Only functions can be decorated with @bind. <${propertyKey}> is not a function!`); | ||
return { | ||
configurable: true, | ||
get(): T { | ||
return descriptor.value.bind(this); | ||
} | ||
}; | ||
} | ||
- It will `throw` exceptions if decorating anything other than `function`; | ||
- Since the implementation follows the latest `decorator`s [purposal](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). | ||
export default bind; | ||
``` | ||
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). | ||
@@ -24,0 +12,0 @@ # Install |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
19
5200
6
78