Newts Parading Majestically
Share your code. npm Orgs help your team discover, share, and reuse code. Create a free org »

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!")
  }
}

Keywords

none

install

npm i es6-class-auto-bind

Downloadsweekly downloads

3

version

1.0.0

license

MIT

last publish

collaborators

  • avatar