12345678910111213141516171819 |
- export function bindMethods<T extends object>(instance: T): void {
- const prototype = Object.getPrototypeOf(instance);
- const propertyNames = Object.getOwnPropertyNames(prototype);
- propertyNames.forEach((propertyName) => {
- const descriptor = Object.getOwnPropertyDescriptor(prototype, propertyName);
- const propertyValue = instance[propertyName as keyof T];
- if (
- typeof propertyValue === 'function' &&
- propertyName !== 'constructor' &&
- descriptor &&
- !descriptor.get &&
- !descriptor.set
- ) {
- instance[propertyName as keyof T] = propertyValue.bind(instance);
- }
- });
- }
|