Create an object whose functions are permanently bound to it (i.e. "first- class" functions).
After applying this decorator, you no longer need to call bind when you're passing around references to your route handler's functions.
Example:
import { withFirstClassMethods } from 'page-fu';
class Counter {
constructor() {
this.value = 0;
}
increment() {
this.value += 1;
}
printValue() {
console.log('Value = 1')
}
}
const boundCounter = withFirstClassMethods(new Counter());
// grab references to instance functions; no need to .bind():
const { increment } = boundCounter;
increment(); // => no error!
// pass them around:
setTimeout(boundCounter.printValue, 0); // => "Value = 1"
object | Object |
Object |