autobind-decorator
Advanced tools
Comparing version 2.1.0 to 2.2.0
@@ -5,2 +5,12 @@ # Change Log | ||
<a name="2.2.0"></a> | ||
# [2.2.0](https://github.com/andreypopp/autobind-decorator/compare/v2.1.0...v2.2.0) (2018-11-02) | ||
### Features | ||
* **module:** export boundMethod and boundClass as modules ([9edeabf](https://github.com/andreypopp/autobind-decorator/commit/9edeabf)) | ||
<a name="2.1.0"></a> | ||
@@ -7,0 +17,0 @@ # [2.1.0](https://github.com/andreypopp/autobind-decorator/compare/v2.0.0...v2.1.0) (2017-07-01) |
@@ -9,59 +9,6 @@ 'use strict'; | ||
exports.boundMethod = boundMethod; | ||
exports.boundClass = boundClass; | ||
exports.default = autobind; | ||
/** | ||
* @copyright 2015, Andrey Popp <8mayday@gmail.com> | ||
* | ||
* The decorator may be used on classes or methods | ||
* ``` | ||
* @autobind | ||
* class FullBound {} | ||
* | ||
* class PartBound { | ||
* @autobind | ||
* method () {} | ||
* } | ||
* ``` | ||
*/ | ||
function autobind() { | ||
if (arguments.length === 1) { | ||
return boundClass.apply(undefined, arguments); | ||
} else { | ||
return boundMethod.apply(undefined, arguments); | ||
} | ||
} | ||
/** | ||
* Use boundMethod to bind all methods on the target.prototype | ||
*/ | ||
function boundClass(target) { | ||
// (Using reflect to get all keys including symbols) | ||
var keys = void 0; | ||
// Use Reflect if exists | ||
if (typeof Reflect !== 'undefined' && typeof Reflect.ownKeys === 'function') { | ||
keys = Reflect.ownKeys(target.prototype); | ||
} else { | ||
keys = Object.getOwnPropertyNames(target.prototype); | ||
// use symbols if support is provided | ||
if (typeof Object.getOwnPropertySymbols === 'function') { | ||
keys = keys.concat(Object.getOwnPropertySymbols(target.prototype)); | ||
} | ||
} | ||
keys.forEach(function (key) { | ||
// Ignore special case target method | ||
if (key === 'constructor') { | ||
return; | ||
} | ||
var descriptor = Object.getOwnPropertyDescriptor(target.prototype, key); | ||
// Only methods need binding | ||
if (typeof descriptor.value === 'function') { | ||
Object.defineProperty(target.prototype, key, boundMethod(target, key, descriptor)); | ||
} | ||
}); | ||
return target; | ||
} | ||
/** | ||
* Return a descriptor removing the value and returning a getter | ||
@@ -110,1 +57,42 @@ * The getter will return a .bind version of the function | ||
} | ||
/** | ||
* Use boundMethod to bind all methods on the target.prototype | ||
*/ | ||
function boundClass(target) { | ||
// (Using reflect to get all keys including symbols) | ||
var keys = void 0; | ||
// Use Reflect if exists | ||
if (typeof Reflect !== 'undefined' && typeof Reflect.ownKeys === 'function') { | ||
keys = Reflect.ownKeys(target.prototype); | ||
} else { | ||
keys = Object.getOwnPropertyNames(target.prototype); | ||
// use symbols if support is provided | ||
if (typeof Object.getOwnPropertySymbols === 'function') { | ||
keys = keys.concat(Object.getOwnPropertySymbols(target.prototype)); | ||
} | ||
} | ||
keys.forEach(function (key) { | ||
// Ignore special case target method | ||
if (key === 'constructor') { | ||
return; | ||
} | ||
var descriptor = Object.getOwnPropertyDescriptor(target.prototype, key); | ||
// Only methods need binding | ||
if (typeof descriptor.value === 'function') { | ||
Object.defineProperty(target.prototype, key, boundMethod(target, key, descriptor)); | ||
} | ||
}); | ||
return target; | ||
} | ||
function autobind() { | ||
if (arguments.length === 1) { | ||
return boundClass.apply(undefined, arguments); | ||
} else { | ||
return boundMethod.apply(undefined, arguments); | ||
} | ||
} |
{ | ||
"name": "autobind-decorator", | ||
"version": "2.1.0", | ||
"version": "2.2.0", | ||
"description": "Decorator for binding method to an object", | ||
"main": "lib/index.js", | ||
"module": "src/index.js", | ||
"types": "./index.d.ts", | ||
@@ -13,3 +14,3 @@ "scripts": { | ||
"clean": "make clean", | ||
"preversion": "npm run build && npm run build-test && npm run lint && npm run test" | ||
"release": "make release" | ||
}, | ||
@@ -16,0 +17,0 @@ "author": "Andrey Popp <8mayday@gmail.com>", |
@@ -7,2 +7,4 @@ # autobind decorator | ||
`autobind` is lazy and is only bound once. :thumbsup: | ||
```js | ||
@@ -22,2 +24,6 @@ // Before: | ||
**Note TypeScript users:** | ||
This package will work out of the box with TypeScript (no Babel needed) and includes the `.d.ts` typings along with it. | ||
## Installation: | ||
@@ -31,3 +37,28 @@ | ||
### Recommended way to bind a method: | ||
```js | ||
import {boundMethod} from 'autobind-decorator' | ||
class Component { | ||
constructor(value) { | ||
this.value = value | ||
} | ||
@boundMethod | ||
method() { | ||
return this.value | ||
} | ||
} | ||
let component = new Component(42) | ||
let method = component.method // .bind(component) isn't needed! | ||
method() // returns 42 | ||
``` | ||
`@boundMethod` makes `method` into an auto-bound method, replacing the explicit bind call later. | ||
### Legacy approaches: | ||
```js | ||
import autobind from 'autobind-decorator' | ||
@@ -50,4 +81,4 @@ | ||
// Also usable on the class to bind all methods | ||
// Please see performance if you decide to autobind your class | ||
@autobind | ||
@@ -57,4 +88,44 @@ class Component { } | ||
```js | ||
import {boundClass} from 'autobind-decorator' | ||
@boundClass | ||
class Component { | ||
constructor(value) { | ||
this.value = value | ||
} | ||
method() { | ||
return this.value | ||
} | ||
} | ||
let component = new Component(42) | ||
let method = component.method // .bind(component) isn't needed! | ||
method() // returns 42 | ||
``` | ||
## Performance | ||
`autobind` (`boundMethod`) on a method is lazy and is only bound once. :thumbsup: | ||
However, | ||
> It is unnecessary to do that to every function. This is just as bad as autobinding (on a class). You only need to bind functions that you pass around. e.g. `onClick={this.doSomething}`. Or `fetch.then(this.handleDone)` | ||
-- Dan Abramov | ||
You should avoid using `autobind` (`boundClass`) on a class. :thumbsdown: | ||
> I was the guy who came up with | ||
autobinding in older Reacts and I'm glad | ||
to see it gone. It might save you a few | ||
keystrokes but it allocates functions | ||
that'll never be called in 90% of cases | ||
and has noticeable performance | ||
degradation. Getting rid of autobinding | ||
is a good thing | ||
-- Peter Hunt | ||
## Alternative | ||
You might want to look at [Class instance properties](https://tc39.github.io/proposal-class-public-fields/). |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
24799
162
127
0