Skip to content

Utility function that copies-over all static methods of a child object to the parent object.

License

Notifications You must be signed in to change notification settings

aprilmintacpineda/redefine-statics-js

Repository files navigation

redefine-statics-js

Utility function that copies-over all static methods of a child object to the parent object.

The problem

This utility solves: The problem is that when we have a class and we wrap it inside a higher-order-function, the static methods of the wrapped class should be copied to the higher-order-class (the class returned by the higher-order-function).

Usage

npm i -s redefine-statics-js.

Then on your wrapper:

import redefineStatics from 'redefine-statics-js';

function MyWrapper(WrappedClass) {
  class MyWrapperClass {
    // some methods here.
  }

  redefineStatics(MyWrapperClass, WrappedClass);

  return MyWrapperClass;
}

Then on the wrapped class:

class WrappedObject {
  static myStaticMethod() {}
}

export default MyWrapper(WrappedObject);

Now the export of the wrapped class would have myStaticMethod in it.