class-to-invokable.js 561 B

123456789101112131415161718192021222324
  1. 'use strict';
  2. /**
  3. * Wraps a constructor to not need the `new` keyword using a proxy.
  4. * Only used for data types.
  5. *
  6. * @param {Function} Class The class instance to wrap as invocable.
  7. * @returns {Proxy} Wrapped class instance.
  8. * @private
  9. */
  10. function classToInvokable(Class) {
  11. return new Proxy(Class, {
  12. apply(Target, thisArg, args) {
  13. return new Target(...args);
  14. },
  15. construct(Target, args) {
  16. return new Target(...args);
  17. },
  18. get(target, p) {
  19. return target[p];
  20. }
  21. });
  22. }
  23. exports.classToInvokable = classToInvokable;