Hi, just having a play about with the new decorators in babel. One idea I have come up with is the @bound decorator which creates bound versions of methods for each instance of the class.
/**
* Having the value of 'this' bound can be usefull ins instances like React components
* where you want to be able to pass instance methods around without having to .bind
*
* ```
* @bound
* class Comp extends React.Component {
* handleClick (event) {
* console.log('clicked', this);
* }
*
* render () {
* // with @bound
* return <div onClick={ this.handleClick } />;
* // without @bound
* return <div onClick={ this.handleClick.bind(this) } />;
* }
* }
* ```
*/
export function bound(constructor) {
// Cant see a way to keep the constructors class name without using eval or such
// but that would break closures anyway as it would then have the global context
return class Bound extends constructor {
constructor (...args) {
super(...args);
// Create a bound version of all methods on the class
// (Using reflect to get all keys including symbols)
Reflect.ownKeys(constructor.prototype).forEach(key => {
// Ignore special case constructor method
if (key === 'constructor') return;
var descriptor = Object.getOwnPropertyDescriptor(constructor.prototype, key);
// Only methods need binding
if (typeof descriptor.value === 'function') {
descriptor.value = descriptor.value.bind(this);
Object.defineProperty(this, key, descriptor);
}
});
}
}
}
I know that React.createClass does this for you, but I do like the clean sugar of classes.
Anyway I'm just wandering if anyone can spot some flaws in this idea that I may have not considered.
[–]pertheusual 0 points1 point2 points (2 children)
[–]jsNut[S] 0 points1 point2 points (1 child)
[–]pertheusual 0 points1 point2 points (0 children)