protobind
v1.0.12
Published
Binds all methods on an object to itself, including those defined on its prototype, and inherited up the chain.
Downloads
15
Keywords
Readme
protobind
Binds all methods on an object to itself, including those defined on its prototype, and inherited up the chain. Also ensures that any methods' properties are preserved.
import autobind from 'protobind';
class Foo {
constructor() {
autobind(this);
}
foo() {
return 'stuff';
}
bar() {
return this.foo();
}
}
const bar = new Foo().bar;
bar(); // 'stuff'
You can also use it as a decorator:
import autobind from 'protobind';
@autobind
class Foo {
foo() {
return 'stuff';
}
bar() {
return this.foo();
}
}
const bar = new Foo().bar;
bar(); // 'stuff'