es6-class-auto-bind

1.0.0 • Public • Published

ES6 Class Auto Bind

A helpful ES6 base class that auto-binds methods to instances. You can restrict the auto-binding to a regular expression and control the next base class in the inheritance chain.

class SimpleClass extends AutoBind() {
  constructor(name) {
    this.name = name
  }
  toString() {
    return this.name
  }
}
 
let o = new SimpleClass("foo")
let f = f.toString
console.log(f())

Maybe you only want to auto bind event handlers based on onEvent type method names?

export class Button extends AutoBind(/^on[A-Z]*$/) {
  onClick() {
    console.log("clicked!")
  }
}

Of course, you might already have a class you need to inherit from. AutoBind lets you control what class it inherits from, but its auto-binding behavior will only apply to your class.

export class Copyright extends AutoBind(/^on[A-Z]*$/, React.Component) {
  constructor(props={}) {
    super(props)
  }
  render() {
    return <div>Copyright &copy; 2016</div>
  }
  onClick() {
    console.log("clicked!")
  }
}

Dependencies (0)

    Dev Dependencies (38)

    Package Sidebar

    Install

    npm i es6-class-auto-bind

    Weekly Downloads

    1

    Version

    1.0.0

    License

    MIT

    Last publish

    Collaborators

    • ironfroggy