编辑:我将关闭此问题作为 this 的重复问题.
我无法理解这个简单的概念。我想创建一个类,例如:
function InputExt() {
...
}
然后我想做这样的事情:
InputExt.prototype = document.createElement('input');
但这只会给我的 InputExt
方法和 Node
属性,而 InputExt
实例不会表现为 Node
,也不是 HTMLInputElement
。
如何实现以下行为?
InputExt.prototype.info = function() {
console.log(this);
}
var element = document.body.appendChild(new InputExt());
element.info();
最佳答案
function InputExt() {
var element = document.createElement('input');
for (prop in InputExt.prototype) {
element[prop] = InputExt.prototype[prop];
}
return element;
}
InputExt.prototype.info = function() {
console.log(this);
}
var element = document.body.appendChild(new InputExt());
element.info();
关于javascript - 如何创建我自己的类的实例,但使其行为像节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35598779/