DEV Community

Discussion on: What is the use of Proxy and Reflect in JavaScript?

Collapse
 
peerreynders profile image
peerreynders

First time I came across Reflect was in the web components native shim:

  const wrapperForTheName = {
    'HTMLElement': function HTMLElement(this: HTMLElement) {
      return Reflect.construct(BuiltInHTMLElement, [], this.constructor);
    },
  };
  window.HTMLElement = (wrapperForTheName[
    'HTMLElement'
  ] as unknown) as typeof HTMLElement;
  HTMLElement.prototype = BuiltInHTMLElement.prototype;
  HTMLElement.prototype.constructor = HTMLElement;
  Object.setPrototypeOf(HTMLElement, BuiltInHTMLElement);
Enter fullscreen mode Exit fullscreen mode

MDN: Reflect.construct().

Browsers implement custom elements as native classes (separate from JS classes).

Some tooling insists on compiling down to ES5 and using Reflect.construct() instead of new makes it possible to use custom elements from ES5 code (obviously the browser has to support custom elements which implies that it also supports classes (ES2015)).


Collapse
 
rajeshroyal profile image
Rajesh Royal

a good one 👍