{"id":471612,"date":"2025-08-22T09:01:25","date_gmt":"2025-08-22T09:01:25","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=471612"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=471612","title":{"rendered":"<span>JavaScript: \u0445\u043e\u0447\u0443 \u0441\u0432\u043e\u0439 HTMLElement<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041f\u0440\u043e\u0441\u0442\u043e \u0445\u043e\u0447\u0443 \u0441\u0442\u0440\u043e\u0438\u0442\u044c \u0441\u0432\u043e\u0439 DOM \u0438\u0437 \u0441\u0432\u043e\u0438\u0445 \u043a\u0438\u0440\u043f\u0438\u0447\u0435\u0439.<br \/>\u0421 \u043f\u0440\u0435\u0444\u0435\u0440\u0430\u043d\u0441\u043e\u043c \u0438 \u043f\u043e\u044d\u0442\u0435\u0441\u0441\u0430\u043c\u0438&#8230;<br \/>\u0418, \u0435\u0441\u043b\u0438 \u0443\u0436 \u043d\u0430 \u0442\u043e \u043f\u043e\u0448\u043b\u043e, \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0447\u0442\u043e-\u0442\u043e \u0442\u0438\u043f\u0430: &#171;\u0440\u0430\u0437 \u043f\u043e\u0448\u043b\u0430 \u0442\u0430\u043a\u0430\u044f \u043f\u044a\u044f\u043d\u043a\u0430&#8230;&#187;<\/p>\n<p>\u0414\u0443\u043c\u0430\u044e \u043d\u0435\u043a\u0442\u043e\u0440\u044b\u0435 \u043f\u043e\u043d\u0438\u043c\u0430\u044e\u0442, \u0447\u0442\u043e \u0442\u0430\u043a \u043c\u043e\u0436\u043d\u043e, \u043d\u043e &#8212; \u043f\u043e\u0432\u0442\u043e\u0440\u0435\u043d\u0438\u0435 \u043c\u0430\u0442\u044c \u0443\u0447\u0435\u043d\u0438\u044f, \u0438, \u0442\u043e \u0435\u0441\u0442\u044c, \u043d\u0438\u043a\u0442\u043e \u043d\u0435 \u043c\u0435\u0448\u0430\u0435\u0442 \u0438 \u043d\u0435 \u043c\u0435\u0448\u0430\u043b \u0434\u0435\u043b\u0430\u0442\u044c \u043d\u0435 \u0442\u0430\u043a, \u043a\u0430\u043a \u0432\u0441\u0435 \u043f\u0440\u0438\u0432\u044b\u043a\u043b\u0438, \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0447\u0435\u0439-\u0442\u043e \u0433\u043e\u0442\u043e\u0432\u044b\u0439 \u043a\u043e\u0434, \u0438 \u043d\u0435 \u043e\u0441\u0442\u0430\u0432\u0430\u0442\u044c\u0441\u044f \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0439, \u043d\u0430\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u0445 \u043a\u0435\u043c-\u0442\u043e \u043d\u0430 \u0447\u0442\u043e-\u0442\u043e &#171;\u043f\u0440\u043e\u0441\u0442\u043e \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e&#187;.<\/p>\n<p>\u0427\u0442\u043e \u043c\u043d\u0435 \u044d\u0442\u043e \u0434\u0430\u0441\u0442:<br \/>&#8212; \u043c\u043d\u0435 \u0431\u043e\u043b\u044c\u0448\u0435 \u043d\u0435 \u043d\u0443\u0436\u043d\u044b \u043f\u043e\u0434\u043f\u0438\u0441\u043a\u0438, \u043c\u043e\u0433\u0443 \u043f\u0440\u043e\u0441\u0442\u043e \u0437\u043d\u0430\u0442\u044c, \u0447\u0442\u043e \u043a\u0430\u043a\u043e\u0435-\u0442\u043e \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u043e \u0438\u0437\u043c\u0435\u043d\u0438\u043b\u043e\u0441\u044c<br \/>&#8212; \u043c\u043e\u0433\u0443 \u0443\u0441\u0442\u0440\u0430\u0438\u0432\u0430\u0442\u044c \u043a\u043e\u043c\u043c\u0443\u043d\u0438\u043a\u0430\u0446\u0438\u0438 \u0441 \u043d\u043e\u0434\u0430\u043c\u0438 \u043d\u0430 \u0441\u0432\u043e\u0451 \u0443\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u0438\u0435<br \/>&#8212; \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u0443\u044e \u043b\u043e\u0433\u0438\u043a\u0443 \u0432\u043e\u043e\u0431\u0449\u0435 \u043d\u0435 \u0442\u0440\u043e\u0433\u0430\u044e \u0438 \u043d\u0435 \u043c\u0435\u0448\u0430\u044e \u0435\u0439<\/p>\n<p>\u041a\u0430\u043a ? \u0412\u043e\u0437\u044c\u043c\u0443 Proxy \u0438 \u0431\u0443\u0434\u0443 \u043e\u0431\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u0442\u044c \u0432 \u043d\u0435\u0433\u043e \u0432\u0441\u0451, \u0447\u0442\u043e \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e.<\/p>\n<p>\u0418\u0442\u043e\u0433\u043e, \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c &#171;\u0431\u0430\u0437\u0443&#187; \u0434\u043b\u044f \u043d\u0430\u0448\u0435\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430:<\/p>\n<pre><code class=\"javascript\">const { HTMLElement } = window;  class MyHTMLElement extends HTMLElement {      constructor() {         super();     }  }  customElements.define('my-custom-element', MyHTMLElement); const myElement = document.createElement('my-custom-element');  console.log('myElement instanceof MyHTMLElement',     myElement instanceof MyHTMLElement);   \/\/ true console.log('myElement instanceof HTMLElement',     myElement instanceof HTMLElement);      \/\/ true<\/code><\/pre>\n<p>\u0414\u0430, \u0442\u0443\u0442 \u0448\u0442\u0443\u043a\u0430 \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u043d\u0443\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <code>customElements.define<\/code> \u0438 \u0431\u0435\u0437 \u043d\u0435\u0433\u043e \u043d\u0438\u043a\u0430\u043a \u043d\u0435\u043b\u044c\u0437\u044f, \u0442\u0430\u043a \u043a\u0430\u043a \u043e\u043d\u043e, \u0432 \u0446\u0435\u043b\u043e\u043c, \u043a\u0430\u043a \u0431\u044b \u0437\u0430\u043f\u0440\u0435\u0449\u0435\u043d\u043e, \u0431\u0435\u0437 \u044d\u0442\u043e\u0433\u043e \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u043e\u0448\u0438\u0431\u043a\u0443:<\/p>\n<blockquote>\n<p><strong>TypeError: Failed to construct &#8216;HTMLElement&#8217;: Illegal construct<\/strong><\/p>\n<\/blockquote>\n<p>\u041d\u043e \u0432 \u0446\u0435\u043b\u043e\u043c \u043d\u0430\u0441 \u044d\u0442\u043e \u043d\u0438\u043a\u0430\u043a \u043e\u0441\u043e\u0431\u043e \u043d\u0435 \u043d\u0430\u043f\u0440\u044f\u0433\u0430\u0435\u0442, \u043f\u043e\u044d\u0442\u043c\u043e\u0443 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0438\u043c.<br \/>\u0422\u0435\u043f\u0435\u0440\u044c \u043c\u043e\u0436\u043d\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c Proxy \u0434\u043b\u044f \u043e\u0442\u0441\u043b\u0435\u0436\u0438\u0432\u0430\u043d\u0438\u044f \u043e\u0431\u0440\u0430\u0449\u0435\u043d\u0438\u0439 \u043a \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430\u043c.<\/p>\n<pre><code class=\"javascript\">const { HTMLElement } = window;  class MyHTMLElement extends HTMLElement {      constructor() {         super();     }  }  const protoProps = {}; Object.setPrototypeOf(protoProps, HTMLElement.prototype);  const proxy = new Proxy(protoProps, {     get(target, prop, receiver) {         const result = Reflect.get(target, prop, receiver);         console.log('get:', prop, result);         return result;     },     set(target, prop, value, receiver) {         const result = Reflect.set(target, prop, value, receiver);         console.log('set:', prop, result, value);         return result;     }, });   Object.setPrototypeOf(MyHTMLElement.prototype, proxy);  customElements.define('my-custom-element', MyHTMLElement); const myElement = document.createElement('my-custom-element');  console.log('myElement instanceof MyHTMLElement',     myElement instanceof MyHTMLElement); console.log('myElement instanceof HTMLElement',     myElement instanceof HTMLElement);<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0443\u0431\u0435\u0434\u0438\u043c\u0441\u044f, \u0447\u0442\u043e \u0432\u0441\u0451 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043a\u0430\u043a \u0437\u0430\u0434\u0443\u043c\u0430\u043d\u043e, \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043f\u0440\u043e\u0441\u0442\u043e\u0439 HTML \u0438 \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u0443\u044e \u043e\u0431\u0432\u044f\u0437\u043a\u0443.<\/p>\n<p><strong>index.html<\/strong> :<\/p>\n<pre><code class=\"xml\">&lt;html&gt;  &lt;head&gt;     &lt;style&gt;         #some {             padding: auto;             text-align: center;             border: 1px solid red;             min-height: 100px;             font-size: 7vh;         }     &lt;\/style&gt; &lt;\/head&gt;  &lt;body bgcolor=\"white\"&gt;     &lt;div id=\"some\"&gt;&lt;\/div&gt;     &lt;script src=\"MyHTMLElement.js\"&gt;&lt;\/script&gt; &lt;\/body&gt;  &lt;\/html&gt;<\/code><\/pre>\n<p><strong>MyHTMLElement.js<\/strong> :<\/p>\n<pre><code class=\"javascript\"> const { HTMLElement } = window;  class MyHTMLElement extends HTMLElement {      constructor() {         super();     }  }  const protoProps = {}; Object.setPrototypeOf(protoProps, HTMLElement.prototype);  const proxy = new Proxy(protoProps, {     get(target, prop, receiver) {         const result = Reflect.get(target, prop, receiver);         console.log('get:', prop, result);         return result;     },     set(target, prop, value, receiver) {         const result = Reflect.set(target, prop, value, receiver);         console.log('set:', prop, result, value);         return result;     }, });   Object.setPrototypeOf(MyHTMLElement.prototype, proxy);  customElements.define('my-custom-element', MyHTMLElement); const myElement = document.createElement('my-custom-element');  console.log('myElement instanceof MyHTMLElement',     myElement instanceof MyHTMLElement); console.log('myElement instanceof HTMLElement',     myElement instanceof HTMLElement);  console.log('render begins'); myElement.innerText = 123; const renderBox = document.getElementById('some'); renderBox.appendChild(myElement); console.log('render finish'); <\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0432 \u043a\u043e\u043d\u0441\u043e\u043b\u0438 &#171;\u0432\u0438\u0434\u043d\u043e \u0432\u0441\u0451&#187; :<\/p>\n<figure class=\"full-width\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/698\/acc\/1c8\/698acc1c89bf0acef1a575bfa2ad38a2.png\" width=\"803\" height=\"317\" sizes=\"auto, (max-width: 780px) 100vw, 50vw\" srcset=\"https:\/\/habrastorage.org\/r\/w780\/getpro\/habr\/upload_files\/698\/acc\/1c8\/698acc1c89bf0acef1a575bfa2ad38a2.png 780w,&#10;       https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/698\/acc\/1c8\/698acc1c89bf0acef1a575bfa2ad38a2.png 781w\" loading=\"lazy\" decode=\"async\"\/><\/figure>\n<p>\u041a\u043e\u043d\u0435\u0447\u043d\u043e, \u0442\u0435\u043f\u0435\u0440\u044c \u043d\u0430\u043c \u043d\u0438\u043a\u0442\u043e \u043d\u0435 \u0437\u0430\u043f\u0440\u0435\u0449\u0430\u0435\u0442 \u0438\u0433\u0440\u0430\u0442\u044c\u0441\u044f \u0441 \u043d\u0438\u043c \u0442\u0430\u043a, \u043a\u0430\u043a \u043d\u0430\u043c \u0432\u0437\u0434\u0443\u043c\u0430\u0435\u0442\u0441\u044f, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0432 \u043d\u0435\u0445\u043e \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0445 \u0441\u0432\u043e\u0439\u0441\u0442\u0432 \u0438 \u043d\u0430\u043b\u0430\u0434\u0438\u0442\u044c \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 \u043f\u0440\u044f\u043c\u043e\u0439 \u043a\u0430\u043d\u0430\u043b \u043e\u0431\u0449\u0435\u043d\u0438\u044f \u0434\u043b\u044f \u0432\u043d\u0435\u0448\u043d\u0438\u0445 \u043a\u043e\u043c\u043c\u0443\u043d\u0438\u043a\u0430\u0446\u0438\u0439:<\/p>\n<pre><code class=\"javascript\"> const { HTMLElement } = window;  class MyHTMLElement extends HTMLElement {      communicate(value) {         this.innerHTML = `${this.protoAddition} + ${this.addition} + ${value}`;     }      addition = 'addition';      constructor() {         super();     }  }  const protoProps = {     protoAddition: 'protoAddition', }; Object.setPrototypeOf(protoProps, HTMLElement.prototype);  const proxy = new Proxy(protoProps, {     get(target, prop, receiver) {         const result = Reflect.get(target, prop, receiver);         console.log('get:', prop, result);         return result;     },     set(target, prop, value, receiver) {         const result = Reflect.set(target, prop, value, receiver);         console.log('set:', prop, result, value);         return result;     }, });   Object.setPrototypeOf(MyHTMLElement.prototype, proxy);  customElements.define('my-custom-element', MyHTMLElement); const myElement = document.createElement('my-custom-element');  console.log('myElement instanceof MyHTMLElement',     myElement instanceof MyHTMLElement); console.log('myElement instanceof HTMLElement',     myElement instanceof HTMLElement);  console.log('render begins'); myElement.innerText = 123;          \/\/ set   const renderBox = document.getElementById('some'); renderBox.appendChild(myElement); console.log('render finish');  myElement.communicate('message');  console.log(myElement.innerText);   \/\/ get<\/code><\/pre>\n<p>\u0415\u0441\u043b\u0438 \u043d\u0443\u0436\u043d\u0430 \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 Gist, \u0442\u043e <a href=\"https:\/\/gist.github.com\/wentout\/057304bcaeb17e3fb4bc16efe719a55d\" rel=\"noopener noreferrer nofollow\"><strong>\u0414\u0435\u0440\u0436\u0438\u0442\u0435<\/strong><\/a> .<\/p>\n<p>\u041d\u0430\u0434\u0435\u044e\u0441\u044c, \u0447\u0442\u043e \u0442\u0435\u043f\u0435\u0440\u044c \u043a\u043e\u0434 \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0445 Front-End \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a \u0431\u0443\u0434\u0435\u0442 \u043c\u0435\u043d\u0435\u0435 \u043f\u0443\u0433\u0430\u044e\u0449\u0438\u043c \u0438 \u0437\u0430\u0433\u0430\u0434\u043e\u0447\u043d\u044b\u043c, \u043d\u0435\u0442 \u043d\u0438\u043a\u0430\u043a\u043e\u0439 \u043c\u0430\u0433\u0438\u0438, \u0432\u0441\u0451 \u0431\u0430\u043d\u0430\u043b\u044c\u043d\u043e \u0438 \u043d\u0435 \u043e\u0447\u0435\u043d\u044c \u0441\u043b\u043e\u0436\u043d\u043e.<\/p>\n<p>\u041a\u043e\u043d\u0435\u0447\u043d\u043e, \u0435\u0441\u0442\u044c \u0438 \u0434\u0440\u0443\u0433\u0438\u0435 API \u0441\u043e \u0441\u0445\u043e\u0436\u0438\u043c \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u0435\u043c, Listeners, Observables \u0438 \u0442.\u043f. \u0415\u0441\u0442\u044c \u043f\u0440\u0435\u0438\u043c\u0443\u0449\u0435\u0441\u0442\u0432\u0430, \u0435\u0441\u0442\u044c \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043a\u0438. \u041d\u043e \u0435\u0441\u043b\u0438 \u0445\u043e\u0447\u0435\u0442\u0441\u044f \u0447\u0435\u0433\u043e-\u0442\u043e &#171;\u043f\u0440\u043e\u0441\u0442\u043e\u0433\u043e \u043a\u0430\u043a \u0442\u043e\u043f\u043e\u0440&#187;, \u0442\u043e \u0432\u043e\u0442.<\/p>\n<p>\u0421\u043f\u0430\u0441\u0438\u0431\u043e \u0437\u0430 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 )<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/939728\/\"> https:\/\/habr.com\/ru\/articles\/939728\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041f\u0440\u043e\u0441\u0442\u043e \u0445\u043e\u0447\u0443 \u0441\u0442\u0440\u043e\u0438\u0442\u044c \u0441\u0432\u043e\u0439 DOM \u0438\u0437 \u0441\u0432\u043e\u0438\u0445 \u043a\u0438\u0440\u043f\u0438\u0447\u0435\u0439.<br \/>\u0421 \u043f\u0440\u0435\u0444\u0435\u0440\u0430\u043d\u0441\u043e\u043c \u0438 \u043f\u043e\u044d\u0442\u0435\u0441\u0441\u0430\u043c\u0438&#8230;<br \/>\u0418, \u0435\u0441\u043b\u0438 \u0443\u0436 \u043d\u0430 \u0442\u043e \u043f\u043e\u0448\u043b\u043e, \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0447\u0442\u043e-\u0442\u043e \u0442\u0438\u043f\u0430: &#171;\u0440\u0430\u0437 \u043f\u043e\u0448\u043b\u0430 \u0442\u0430\u043a\u0430\u044f \u043f\u044a\u044f\u043d\u043a\u0430&#8230;&#187;<\/p>\n<p>\u0414\u0443\u043c\u0430\u044e \u043d\u0435\u043a\u0442\u043e\u0440\u044b\u0435 \u043f\u043e\u043d\u0438\u043c\u0430\u044e\u0442, \u0447\u0442\u043e \u0442\u0430\u043a \u043c\u043e\u0436\u043d\u043e, \u043d\u043e &#8212; \u043f\u043e\u0432\u0442\u043e\u0440\u0435\u043d\u0438\u0435 \u043c\u0430\u0442\u044c \u0443\u0447\u0435\u043d\u0438\u044f, \u0438, \u0442\u043e \u0435\u0441\u0442\u044c, \u043d\u0438\u043a\u0442\u043e \u043d\u0435 \u043c\u0435\u0448\u0430\u0435\u0442 \u0438 \u043d\u0435 \u043c\u0435\u0448\u0430\u043b \u0434\u0435\u043b\u0430\u0442\u044c \u043d\u0435 \u0442\u0430\u043a, \u043a\u0430\u043a \u0432\u0441\u0435 \u043f\u0440\u0438\u0432\u044b\u043a\u043b\u0438, \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0447\u0435\u0439-\u0442\u043e \u0433\u043e\u0442\u043e\u0432\u044b\u0439 \u043a\u043e\u0434, \u0438 \u043d\u0435 \u043e\u0441\u0442\u0430\u0432\u0430\u0442\u044c\u0441\u044f \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0439, \u043d\u0430\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u0445 \u043a\u0435\u043c-\u0442\u043e \u043d\u0430 \u0447\u0442\u043e-\u0442\u043e &#171;\u043f\u0440\u043e\u0441\u0442\u043e \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e&#187;.<\/p>\n<p>\u0427\u0442\u043e \u043c\u043d\u0435 \u044d\u0442\u043e \u0434\u0430\u0441\u0442:<br \/>&#8212; \u043c\u043d\u0435 \u0431\u043e\u043b\u044c\u0448\u0435 \u043d\u0435 \u043d\u0443\u0436\u043d\u044b \u043f\u043e\u0434\u043f\u0438\u0441\u043a\u0438, \u043c\u043e\u0433\u0443 \u043f\u0440\u043e\u0441\u0442\u043e \u0437\u043d\u0430\u0442\u044c, \u0447\u0442\u043e \u043a\u0430\u043a\u043e\u0435-\u0442\u043e \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u043e \u0438\u0437\u043c\u0435\u043d\u0438\u043b\u043e\u0441\u044c<br \/>&#8212; \u043c\u043e\u0433\u0443 \u0443\u0441\u0442\u0440\u0430\u0438\u0432\u0430\u0442\u044c \u043a\u043e\u043c\u043c\u0443\u043d\u0438\u043a\u0430\u0446\u0438\u0438 \u0441 \u043d\u043e\u0434\u0430\u043c\u0438 \u043d\u0430 \u0441\u0432\u043e\u0451 \u0443\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u0438\u0435<br \/>&#8212; \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u0443\u044e \u043b\u043e\u0433\u0438\u043a\u0443 \u0432\u043e\u043e\u0431\u0449\u0435 \u043d\u0435 \u0442\u0440\u043e\u0433\u0430\u044e \u0438 \u043d\u0435 \u043c\u0435\u0448\u0430\u044e \u0435\u0439<\/p>\n<p>\u041a\u0430\u043a ? \u0412\u043e\u0437\u044c\u043c\u0443 Proxy \u0438 \u0431\u0443\u0434\u0443 \u043e\u0431\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u0442\u044c \u0432 \u043d\u0435\u0433\u043e \u0432\u0441\u0451, \u0447\u0442\u043e \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e.<\/p>\n<p>\u0418\u0442\u043e\u0433\u043e, \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c &#171;\u0431\u0430\u0437\u0443&#187; \u0434\u043b\u044f \u043d\u0430\u0448\u0435\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430:<\/p>\n<pre><code class=\"javascript\">const { HTMLElement } = window;  class MyHTMLElement extends HTMLElement {      constructor() {         super();     }  }  customElements.define('my-custom-element', MyHTMLElement); const myElement = document.createElement('my-custom-element');  console.log('myElement instanceof MyHTMLElement',     myElement instanceof MyHTMLElement);   \/\/ true console.log('myElement instanceof HTMLElement',     myElement instanceof HTMLElement);      \/\/ true<\/code><\/pre>\n<p>\u0414\u0430, \u0442\u0443\u0442 \u0448\u0442\u0443\u043a\u0430 \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u043d\u0443\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <code>customElements.define<\/code> \u0438 \u0431\u0435\u0437 \u043d\u0435\u0433\u043e \u043d\u0438\u043a\u0430\u043a \u043d\u0435\u043b\u044c\u0437\u044f, \u0442\u0430\u043a \u043a\u0430\u043a \u043e\u043d\u043e, \u0432 \u0446\u0435\u043b\u043e\u043c, \u043a\u0430\u043a \u0431\u044b \u0437\u0430\u043f\u0440\u0435\u0449\u0435\u043d\u043e, \u0431\u0435\u0437 \u044d\u0442\u043e\u0433\u043e \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u043e\u0448\u0438\u0431\u043a\u0443:<\/p>\n<blockquote>\n<p><strong>TypeError: Failed to construct &#8216;HTMLElement&#8217;: Illegal construct<\/strong><\/p>\n<\/blockquote>\n<p>\u041d\u043e \u0432 \u0446\u0435\u043b\u043e\u043c \u043d\u0430\u0441 \u044d\u0442\u043e \u043d\u0438\u043a\u0430\u043a \u043e\u0441\u043e\u0431\u043e \u043d\u0435 \u043d\u0430\u043f\u0440\u044f\u0433\u0430\u0435\u0442, \u043f\u043e\u044d\u0442\u043c\u043e\u0443 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0438\u043c.<br \/>\u0422\u0435\u043f\u0435\u0440\u044c \u043c\u043e\u0436\u043d\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c Proxy \u0434\u043b\u044f \u043e\u0442\u0441\u043b\u0435\u0436\u0438\u0432\u0430\u043d\u0438\u044f \u043e\u0431\u0440\u0430\u0449\u0435\u043d\u0438\u0439 \u043a \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430\u043c.<\/p>\n<pre><code class=\"javascript\">const { HTMLElement } = window;  class MyHTMLElement extends HTMLElement {      constructor() {         super();     }  }  const protoProps = {}; Object.setPrototypeOf(protoProps, HTMLElement.prototype);  const proxy = new Proxy(protoProps, {     get(target, prop, receiver) {         const result = Reflect.get(target, prop, receiver);         console.log('get:', prop, result);         return result;     },     set(target, prop, value, receiver) {         const result = Reflect.set(target, prop, value, receiver);         console.log('set:', prop, result, value);         return result;     }, });   Object.setPrototypeOf(MyHTMLElement.prototype, proxy);  customElements.define('my-custom-element', MyHTMLElement); const myElement = document.createElement('my-custom-element');  console.log('myElement instanceof MyHTMLElement',     myElement instanceof MyHTMLElement); console.log('myElement instanceof HTMLElement',     myElement instanceof HTMLElement);<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0443\u0431\u0435\u0434\u0438\u043c\u0441\u044f, \u0447\u0442\u043e \u0432\u0441\u0451 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043a\u0430\u043a \u0437\u0430\u0434\u0443\u043c\u0430\u043d\u043e, \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043f\u0440\u043e\u0441\u0442\u043e\u0439 HTML \u0438 \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u0443\u044e \u043e\u0431\u0432\u044f\u0437\u043a\u0443.<\/p>\n<p><strong>index.html<\/strong> :<\/p>\n<pre><code class=\"xml\">&lt;html&gt;  &lt;head&gt;     &lt;style&gt;         #some {             padding: auto;             text-align: center;             border: 1px solid red;             min-height: 100px;             font-size: 7vh;         }     &lt;\/style&gt; &lt;\/head&gt;  &lt;body bgcolor=\"white\"&gt;     &lt;div id=\"some\"&gt;&lt;\/div&gt;     &lt;script src=\"MyHTMLElement.js\"&gt;&lt;\/script&gt; &lt;\/body&gt;  &lt;\/html&gt;<\/code><\/pre>\n<p><strong>MyHTMLElement.js<\/strong> :<\/p>\n<pre><code class=\"javascript\"> const { HTMLElement } = window;  class MyHTMLElement extends HTMLElement {      constructor() {         super();     }  }  const protoProps = {}; Object.setPrototypeOf(protoProps, HTMLElement.prototype);  const proxy = new Proxy(protoProps, {     get(target, prop, receiver) {         const result = Reflect.get(target, prop, receiver);         console.log('get:', prop, result);         return result;     },     set(target, prop, value, receiver) {         const result = Reflect.set(target, prop, value, receiver);         console.log('set:', prop, result, value);         return result;     }, });   Object.setPrototypeOf(MyHTMLElement.prototype, proxy);  customElements.define('my-custom-element', MyHTMLElement); const myElement = document.createElement('my-custom-element');  console.log('myElement instanceof MyHTMLElement',     myElement instanceof MyHTMLElement); console.log('myElement instanceof HTMLElement',     myElement instanceof HTMLElement);  console.log('render begins'); myElement.innerText = 123; const renderBox = document.getElementById('some'); renderBox.appendChild(myElement); console.log('render finish'); <\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0432 \u043a\u043e\u043d\u0441\u043e\u043b\u0438 &#171;\u0432\u0438\u0434\u043d\u043e \u0432\u0441\u0451&#187; :<\/p>\n<figure class=\"full-width\"><\/figure>\n<p>\u041a\u043e\u043d\u0435\u0447\u043d\u043e, \u0442\u0435\u043f\u0435\u0440\u044c \u043d\u0430\u043c \u043d\u0438\u043a\u0442\u043e \u043d\u0435 \u0437\u0430\u043f\u0440\u0435\u0449\u0430\u0435\u0442 \u0438\u0433\u0440\u0430\u0442\u044c\u0441\u044f \u0441 \u043d\u0438\u043c \u0442\u0430\u043a, \u043a\u0430\u043a \u043d\u0430\u043c \u0432\u0437\u0434\u0443\u043c\u0430\u0435\u0442\u0441\u044f, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0432 \u043d\u0435\u0445\u043e \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0445 \u0441\u0432\u043e\u0439\u0441\u0442\u0432 \u0438 \u043d\u0430\u043b\u0430\u0434\u0438\u0442\u044c \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 \u043f\u0440\u044f\u043c\u043e\u0439 \u043a\u0430\u043d\u0430\u043b \u043e\u0431\u0449\u0435\u043d\u0438\u044f \u0434\u043b\u044f \u0432\u043d\u0435\u0448\u043d\u0438\u0445 \u043a\u043e\u043c\u043c\u0443\u043d\u0438\u043a\u0430\u0446\u0438\u0439:<\/p>\n<pre><code class=\"javascript\"> const { HTMLElement } = window;  class MyHTMLElement extends HTMLElement {      communicate(value) {         this.innerHTML = `${this.protoAddition} + ${this.addition} + ${value}`;     }      addition = 'addition';      constructor() {         super();     }  }  const protoProps = {     protoAddition: 'protoAddition', }; Object.setPrototypeOf(protoProps, HTMLElement.prototype);  const proxy = new Proxy(protoProps, {     get(target, prop, receiver) {         const result = Reflect.get(target, prop, receiver);         console.log('get:', prop, result);         return result;     },     set(target, prop, value, receiver) {         const result = Reflect.set(target, prop, value, receiver);         console.log('set:', prop, result, value);         return result;     }, });   Object.setPrototypeOf(MyHTMLElement.prototype, proxy);  customElements.define('my-custom-element', MyHTMLElement); const myElement = document.createElement('my-custom-element');  console.log('myElement instanceof MyHTMLElement',     myElement instanceof MyHTMLElement); console.log('myElement instanceof HTMLElement',     myElement instanceof HTMLElement);  console.log('render begins'); myElement.innerText = 123;          \/\/ set   const renderBox = document.getElementById('some'); renderBox.appendChild(myElement); console.log('render finish');  myElement.communicate('message');  console.log(myElement.innerText);   \/\/ get<\/code><\/pre>\n<p>\u0415\u0441\u043b\u0438 \u043d\u0443\u0436\u043d\u0430 \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 Gist, \u0442\u043e <a href=\"https:\/\/gist.github.com\/wentout\/057304bcaeb17e3fb4bc16efe719a55d\" rel=\"noopener noreferrer nofollow\"><strong>\u0414\u0435\u0440\u0436\u0438\u0442\u0435<\/strong><\/a> .<\/p>\n<p>\u041d\u0430\u0434\u0435\u044e\u0441\u044c, \u0447\u0442\u043e \u0442\u0435\u043f\u0435\u0440\u044c \u043a\u043e\u0434 \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0445 Front-End \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a \u0431\u0443\u0434\u0435\u0442 \u043c\u0435\u043d\u0435\u0435 \u043f\u0443\u0433\u0430\u044e\u0449\u0438\u043c \u0438 \u0437\u0430\u0433\u0430\u0434\u043e\u0447\u043d\u044b\u043c, \u043d\u0435\u0442 \u043d\u0438\u043a\u0430\u043a\u043e\u0439 \u043c\u0430\u0433\u0438\u0438, \u0432\u0441\u0451 \u0431\u0430\u043d\u0430\u043b\u044c\u043d\u043e \u0438 \u043d\u0435 \u043e\u0447\u0435\u043d\u044c \u0441\u043b\u043e\u0436\u043d\u043e.<\/p>\n<p>\u041a\u043e\u043d\u0435\u0447\u043d\u043e, \u0435\u0441\u0442\u044c \u0438 \u0434\u0440\u0443\u0433\u0438\u0435 API \u0441\u043e \u0441\u0445\u043e\u0436\u0438\u043c \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u0435\u043c, Listeners, Observables \u0438 \u0442.\u043f. \u0415\u0441\u0442\u044c \u043f\u0440\u0435\u0438\u043c\u0443\u0449\u0435\u0441\u0442\u0432\u0430, \u0435\u0441\u0442\u044c \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043a\u0438. \u041d\u043e \u0435\u0441\u043b\u0438 \u0445\u043e\u0447\u0435\u0442\u0441\u044f \u0447\u0435\u0433\u043e-\u0442\u043e &#171;\u043f\u0440\u043e\u0441\u0442\u043e\u0433\u043e \u043a\u0430\u043a \u0442\u043e\u043f\u043e\u0440&#187;, \u0442\u043e \u0432\u043e\u0442.<\/p>\n<p>\u0421\u043f\u0430\u0441\u0438\u0431\u043e \u0437\u0430 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 )<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/939728\/\"> https:\/\/habr.com\/ru\/articles\/939728\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-471612","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/471612","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=471612"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/471612\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=471612"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=471612"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=471612"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}