gpt4 book ai didi

javascript - 扩展 HTMLTextAreaElement 的 HTML5 ES6 自定义元素使非法构造函数崩溃

转载 作者:搜寻专家 更新时间:2023-10-30 21:19:33 25 4
gpt4 key购买 nike

我需要从 HTMLTextAreaElement 扩展一个自定义元素,以便在表单中使用并直接获取值。但我总是收到 Illegal Constructor Message

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>

Typescript(编译为 ES6 到 myScript.js):

// 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);

脚本在支持 ES6 和 Custom-Element 的 Chrome 版本 63.0.3239.132 上执行

我已经尝试包括 webcomponents/custom-elements填充

您知道为什么从 HTMLElement 以外的扩展会崩溃吗?

最佳答案

您看到的错误意味着您调用 new 的对象没有 [[construct]] 内部方法。

虽然规范表明您可以扩展 HTML*Element 类,但目前似乎不支持它(参见类似问题:https://github.com/webcomponents/custom-elements/issues/6)所以您只能扩展 HTMLElement此时。

关于javascript - 扩展 HTMLTextAreaElement 的 HTML5 ES6 自定义元素使非法构造函数崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48422748/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com