@knicola/autobind
TypeScript icon, indicating that this package has built-in type declarations

0.1.2 • Public • Published

@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.

Package Sidebar

Install

npm i @knicola/autobind

Weekly Downloads

0

Version

0.1.2

License

MIT

Unpacked Size

5.55 kB

Total Files

9

Last publish

Collaborators

  • knicola