代码之家  ›  专栏  ›  技术社区  ›  hossein sedighian

Extend CustomElement类不工作[重复]

  •  0
  • hossein sedighian  · 技术社区  · 7 年前

    HTML格式:

    <!DOCTYPE html>
    <html>
      <head>
      </head>
      <body>
        <div>
          <working-element></working-element>
        </div>
        <div>
          <crashing-element></crashing-element>
        </div>
      </body>
      <script src="myScript.js">
    </html>
    

    // all works fine
    class newElementWorks extends HTMLElement {
      constructor(){
        super();
        console.log('newElementWorks');
      }
    }
    customElements.define('working-element', newElementWorks);
    
    // this one crashes on super() call
    class newElementCrash extends HTMLTextAreaElement {
      constructor(){
        super();
        console.log('newElementCrash');
      }
    }
    customElements.define('crashing-element', newElementCrash);
    

    该脚本在Chrome版本63.0.3239.132上执行,该版本支持ES6和自定义元素

    我已经准备好了 webcomponents/custom-elements 聚甲醛

    0 回复  |  直到 8 年前
        1
  •  5
  •   adz5A    8 年前

    你看到的错误意味着你正在调用的对象 new [[construct]] 内部方法。

    extend 目前似乎不支持HTML*元素类(请参阅类似问题: https://github.com/webcomponents/custom-elements/issues/6 )所以你只能延长 HTMLElement

    推荐文章