Gathering detailed insights and metrics for class-autobind-decorator
Gathering detailed insights and metrics for class-autobind-decorator
npm install class-autobind-decorator
Typescript
Module System
Node Version
NPM Version
77.7
Supply Chain
99.5
Quality
75.5
Maintenance
100
Vulnerability
100
License
JavaScript (100%)
Total Downloads
885,965
Last Day
301
Last Week
1,784
Last Month
8,550
Last Year
98,587
8 Stars
59 Commits
3 Watching
1 Branches
1 Contributors
Minified
Minified + Gzipped
Latest Version
3.0.1
Package Id
class-autobind-decorator@3.0.1
Size
11.12 kB
NPM Version
5.3.0
Node Version
8.5.0
Publised On
08 Dec 2017
Cumulative downloads
Total Downloads
Last day
-9.1%
301
Compared to previous day
Last week
-19.8%
1,784
Compared to previous week
Last month
1.1%
8,550
Compared to previous month
Last year
-32.8%
98,587
Compared to previous year
A small, customizable, framework-agnostic, ES5-compatible class-level
decorator for automatically binding "class" methods -- i.e., methods on
a constructor's prototype
object -- to instances, so that this
refers to the relevant instance within those methods.
autoBindMethods
decorator, as well as an autoBindMethodsForReact
convenience decorator that skips autobinding methods from the React Component Spec that do not require binding)@autoBindMethods
)
or as a configured decorator (@autoBindMethods(options)
)1npm install --save class-autobind-decorator
Note that this is currently only usable as a "class"-level (or, in ES5
terms, a constructor-function-level) decorator. It can't currently be
used on individual methods. However, the decorator accepts an options
object that can define an array of methods not to bind (using the method
names or Symbol keys; see the use of methodsToIgnore
in the examples
below, as well as the test cases in the tests).
ES6-style as a "legacy" class decorator, without options:
1import autoBindMethods from 'class-autobind-decorator'; 2 3@autoBindMethods // You could also use `@autoBindMethods()` if you want. 4class Foo { 5 someMethod() { 6 return this instanceof Foo; 7 } 8} 9 10const smReference = new Foo().someMethod; 11console.log(smReference()); // => `true`
ES6-style as a "legacy" class decorator, with options:
1import autoBindMethods from 'class-autobind-decorator'; 2// NOTE: For React classes, you can use this alternative: 3// import { autoBindMethodsForReact } from 'class-autobind-decorator'; 4 5@autoBindMethods({ methodsToIgnore: ['unboundMethod', 'render'] }) 6// or: 7// @autoBindMethodsForReact({ methodsToIgnore: ['unboundMethod'] }) 8class MyComponent extends React.Component { 9 someComponentMethod() { 10 // Do something with `this` here. If the React `onClick` 11 // handler below is triggered, `this` will be bound to the 12 // component instance. 13 } 14 15 unboundMethod() { 16 // `this` will not be auto-bound in this method, given the 17 // options passed in to the decorator. 18 } 19 20 // This method will not be bound by `autoBindMethods` either (it 21 // doesn't need to be). 22 render() { 23 return ( 24 <button onClick={this.someComponentMethod} /> 25 ); 26 } 27}
ES5-style, without options:
1var autoBindMethods = require('class-autobind-decorator').default; 2 3var Foo = (function () { 4 function Foo () {} 5 Foo.prototype.someMethod = function () { 6 return this instanceof Foo; 7 }; 8 return Foo; 9}()); 10 11autoBindMethods(Foo); 12var smReference = new Foo().someMethod; 13console.log(smReference()); // => `true`
ES5-style, with options:
1var autoBindMethods = require('class-autobind-decorator').default; 2var customAutoBinder = autoBindMethods({ 3 methodsToIgnore: ['secondMethod'] 4}); 5 6var Foo = (function () { 7 function Foo () {} 8 Foo.prototype.firstMethod = function () { 9 return this instanceof Foo; 10 }; 11 Foo.prototype.secondMethod = function () { 12 return this instanceof Foo; 13 }; 14 return Foo; 15}()); 16 17customAutoBinder(Foo); 18var fooInstance = new Foo(); 19var fmReference = fooInstance.firstMethod; 20var smReference = fooInstance.secondMethod; 21console.log(fmReference()); // => `true` 22console.log(smReference()); // => `false`, due to passed in options
methodsToIgnore: An array of method names that should not be bound if found on the prototype. See the above examples for usage.
dontOptimize: The default behavior of this decorator is to only bind methods to instances once,
and, from that point onward, to store the bound method on the instance itself. You can override
this behavior by setting dontOptimize
to true. If you do that, the method will be re-bound to the
instance on every access; a bound version of the method will not be stored on the instance itself
(so, a use case for this might be if you need the instance not to be modified at all).
Clone the repository, run npm install
, then, in the main (top-level) repo
directory:
1npm run build
Compiled code will be placed in the ./build
directory. You can also
download compiled code directly from this repository.
Clone the repository and go to the main (top-level) repo directory. Be
sure to run npm install
first, and then:
1npm run test
Tests are specified in the ./tests
directory, and use mocha
and
chai
. Running the tests also requires a few extra babel
dependencies
specified in package.json
.
Well, I'm not currently aware of another project that has all of the features mentioned in the "Features" section, above (the ones I am aware of either hard-code React-specific stuff, or don't check whether properties are configurable before trying to redefine them, or inadvertently bind to non-instance objects when methods are accessed first via the prototype, or can't be used as both "bare" (unconfigured) decorators and configured decorators, or don't have TypeScript declarations, and things like that -- no hate, though!). I also just wanted an opportunity to work more directly with decorators, so I used it as a learning experience. :)
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
1 existing vulnerabilities detected
Details
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
Found 0/30 approved changesets -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
license file not detected
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2025-01-27
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More