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.
The best automatic context method binding decorator
throw
exceptions if decorating anything other than function
, run this;decorator
s purposal where compartion betweeen this
and target
can not be trusted, run this if you don't belive me. @bind
will always return
a configurable
, enumerable
get accessor propertyDescriptor
with value of descriptor.value.bind(this)
.In fact the whole implementation is just 12 lines of code:
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);
}
};
}
export default bind;
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'.
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.