DEV Community

Discussion on: Dynamically assigning a function name in javascript

Collapse
 
tonivj5 profile image
Toni Villena • Edited

Very smart tricky! Perfect to decorate a method without lost its original name

export function DecorateMethod(options: {}) {
  return (
    target: any,
    propertyKey: string,
    propertyDescriptor: PropertyDescriptor,
  ) => {
    const method = propertyDescriptor.value as UnknownFunction;

    const { [method.name]: decorated } = {
      [method.name]: () => {...},
    };

    propertyDescriptor.value = decorated;
  };
}
Enter fullscreen mode Exit fullscreen mode

Thanks for publish it ❤️