javascript - 扩展 javascript 元素是否正确?
问题描述
我想为 div 元素创建一个特定的逻辑,并使用构造函数创建它,但我需要使用许多 Element 属性,所以想知道是否有这样的解决方案
class MyElement extends HTMLDivElement {
constructor(father) {
super();
if (father === undefined)
father = document.body;
father.appendChild(this);
this.className = this.constructor.name;
}
}
.
.
.
// Main.js
customElements.define('my-element', MyElement, { extends: 'div' });
或这样的解决方案
class MyElement extends jQuery.fn.init {
constructor(father) {
if (father === undefined)
father = document.body;
super("<div>");
this.$wrapper = null;
this.__proto__ = jQuery.extend(true, this.__proto__, this.__proto__.__proto__)
this.appendTo(father);
this.attr("class", this.constructor.name);
}
}
可能是合理的或“标准的”,以及这些解决方案的优缺点,例如,如果可能存在兼容性问题,或者可能被视为不好的做法
解决方案
推荐阅读
- python - MacOS - Python 3.7 - DirectX 8 (2003) 应用程序的 GUI 鼠标控制 - 无法模拟鼠标点击,但我可以模拟键盘输入
- javascript - 合并两个 Javascript 对象,其中每个键包含多个值
- javascript - Javascript 对象计算属性
- c# - 如何从基类获取派生类类型
- dart - 根据 DartLangSpec-v2.2,类中的静态和非静态变量可能具有相同的名称
- html - 使用 CSS 倒置挖角
- rspec-rails - 无法测试模块内的类[未初始化的常量] Rspec
- reactjs - 绘制画布并使用反应钩子动态更改其大小
- python - 如何在 Numpy Array 和 Pandas Dataframe 之间进行映射?
- sonarqube - React js 中的 Sonarqube 覆盖率为 0%