mirror of
https://github.com/GoogleChromeLabs/squoosh.git
synced 2025-11-12 00:37:19 +00:00
27 lines
702 B
TypeScript
27 lines
702 B
TypeScript
/**
|
|
* A decorator that binds values to their class instance.
|
|
* @example
|
|
* class C {
|
|
* @bind
|
|
* foo () {
|
|
* return this;
|
|
* }
|
|
* }
|
|
* let f = new C().foo;
|
|
* f() instanceof C; // true
|
|
*/
|
|
export function bind(target: any, propertyKey: string, descriptor: PropertyDescriptor) {
|
|
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 bound = descriptor.value.bind(this);
|
|
Object.defineProperty(this, propertyKey, {
|
|
value: bound
|
|
});
|
|
return bound;
|
|
}
|
|
};
|
|
}
|