es6-class-auto-bind
v1.0.0
Published
ES6 Class Auto Binding Utility
Downloads
3
Readme
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 © 2016</div>
}
onClick() {
console.log("clicked!")
}
}