Security News
Introducing the Socket Python SDK
The initial version of the Socket Python SDK is now on PyPI, enabling developers to more easily interact with the Socket REST API in Python projects.
bind-decorator
Advanced tools
The bind-decorator npm package provides a simple way to bind class methods to the instance of the class. This is particularly useful in JavaScript and TypeScript when dealing with event handlers or callbacks where the context of 'this' can be lost.
Method Binding
This feature allows you to bind a class method to the instance of the class using the @bind decorator. This ensures that the method retains the correct 'this' context when called.
class MyClass {
constructor() {
this.value = 42;
}
@bind
printValue() {
console.log(this.value);
}
}
const instance = new MyClass();
const print = instance.printValue;
print(); // Outputs: 42
The autobind-decorator package provides similar functionality by automatically binding methods to the instance of the class. It is also used as a decorator and serves the same purpose of maintaining the correct 'this' context in methods.
The core-decorators package offers a collection of decorators, including @autobind, which binds methods to the instance of the class. It provides additional decorators for other common patterns, making it a more comprehensive solution.
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 decorator
s proposal.
throw
exceptions if decorating anything other than function
;decorator
s proposal 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 for memoizing the result).If you are looking for not just method decorator but rather full class bounding decorator check @autobind
.
Install with npm:
$ npm install bind-decorator
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'.
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'.
npm install
npm test
npm install
Make changes
If necessary add some tests to __tests__
npm test
Make a Pull Request
FAQs
The fastest automatic method.bind(this) decorator
The npm package bind-decorator receives a total of 101,131 weekly downloads. As such, bind-decorator popularity was classified as popular.
We found that bind-decorator demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
The initial version of the Socket Python SDK is now on PyPI, enabling developers to more easily interact with the Socket REST API in Python projects.
Security News
Floating dependency ranges in npm can introduce instability and security risks into your project by allowing unverified or incompatible versions to be installed automatically, leading to unpredictable behavior and potential conflicts.
Security News
A new Rust RFC proposes "Trusted Publishing" for Crates.io, introducing short-lived access tokens via OIDC to improve security and reduce risks associated with long-lived API tokens.