@knicola/autobind
v0.1.2
Published
Autobind methods of a class to the instance
Downloads
4
Maintainers
Readme
@knicola/autobind
Automatically bind all methods of an object to the object itself.
Install
Using npm:
$ npm install @knicola/autobind
Using yarn:
$ yarn add @knicola/autobind
Usage
import { autobind } from '@knicola/autobind'
// with autobind
class Foo {
public myname: string = 'foo'
constructor() {
autobind(this)
}
public say(): void {
console.log(this.myname)
}
}
const { say: sayFoo } = new Foo()
sayFoo() //=> "foo"
// without autobind
class Bar {
public myname: string = 'bar'
public say(): void {
console.log(this.myname)
}
}
const { say: sayBar } = new Bar()
sayBar() //=> [ERR]: this is undefined
License
This project is open-sourced software licensed under the MIT license.