bind-decorator
Advanced tools
Comparing version 1.0.8 to 1.0.9
@@ -1,2 +0,2 @@ | ||
export declare function bind<T>(target: Object, propertyKey: string, 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; |
15
index.js
"use strict"; | ||
var constants; | ||
(function (constants) { | ||
constants.typeOfFunction = 'function'; | ||
constants.boolTrue = true; | ||
})(constants || (constants = {})); | ||
function bind(target, propertyKey, descriptor) { | ||
if (!descriptor || (typeof descriptor.value !== 'function')) { | ||
if (!descriptor || (typeof descriptor.value !== constants.typeOfFunction)) { | ||
throw new TypeError("Only functions can be decorated with @bind. <" + propertyKey + "> is not a function!"); | ||
} | ||
return { | ||
configurable: true, | ||
configurable: constants.boolTrue, | ||
get: function () { | ||
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 }); | ||
Object.defineProperty(this, propertyKey, { | ||
value: bound, | ||
configurable: constants.boolTrue, | ||
writable: constants.boolTrue | ||
}); | ||
return bound; | ||
@@ -13,0 +22,0 @@ } |
{ | ||
"name": "bind-decorator", | ||
"version": "1.0.8", | ||
"version": "1.0.9", | ||
"description": "The fastest automatic method.bind(this) decorator", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -14,6 +14,6 @@ # bind-decorator | ||
`@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/). | ||
`@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 [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). | ||
- 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). | ||
@@ -20,0 +20,0 @@ 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). |
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
7142
29