I've tried to create a subclass of HTMLDivElement, but I've got an error: Uncaught TypeError: Illegal constructor
var subC = function() {
HTMLDivElement.call(this);
//....
};
subC.prototype = Object.create(HTMLDivElement.prototype);
I've tried to create a subclass of HTMLDivElement, but I've got an error: Uncaught TypeError: Illegal constructor
var subC = function() {
HTMLDivElement.call(this);
//....
};
subC.prototype = Object.create(HTMLDivElement.prototype);
How about
function subC() {
//w/e
}
subC.prototype = document.createElement('div');
var x = new subC();
alert(x instanceof HTMLDivElement);
You cant subclass most of the DOM api ,especially things like Node,Element ...
Why? because they involve complex dependency injection,that's why you need to build Element through document.createElement.
The only thing you can do is use composition :
function subC = function(htmlElement){
this.htmlElement=htmlElement;
}
you can then expose some properties of htmlElement in subC
Object.keys(this.htmlElement).forEach(function(k){this[k]=this.htmlElement[k];},this);
in the constructor,as shortcuts.
You'd need to wrap each method you want to use though,i wouldnt risk adding Element.prototype methods to subC and expect them to behave correctly.