Skip to content

Make images smaller using best-in-class codecs, right in the browser.

JavaScript

Our simplified `@bind` decorator didn't work :( The getter is needed in order to defer binding until there is an instance to bind to, the simpler implementation actually bound methods to the prototype itself.

Jason Miller8f452ddf6d9c
diff --git a/src/lib/util.ts b/src/lib/util.ts
index d9765950..12a3e28c 100644
--- a/src/lib/util.ts
+++ b/src/lib/util.ts
@@ -34,6 +34,16 @@ export class When extends Component<WhenProps, WhenState> {
* f() instanceof C; // true
*/
export function bind(target: any, propertyKey: string, descriptor: PropertyDescriptor) {
- descriptor.value = descriptor.value.bind(target);
- return descriptor;
+ return {
+ // the first time the prototype property is accessed for an instance,
+ // define an instance property pointing to the bound function.
+ // This effectively "caches" the bound prototype method as an instance property.
+ get() {
+ let boundFunction = descriptor.value.bind(this);
+ Object.defineProperty(this, propertyKey, {
+ value: boundFunction
+ });
+ return boundFunction;
+ }
+ };
}