How to inherit from the DOM element class

It’s not a good idea to do this.

First of all, to inherit from DOM element, you need to have access to that element’s prototype. The problem is that not all browsers provide access to prototypes of DOM elements. Newer Gecko and WebKit -based clients, for example, expose some of these prototypes as global objects – HTMLDivElement, HTMLElement, Element, Node, etc.

For example, plain DIV element usually has a prototype chain similar to:

HTMLDivElement.prototype -> HTMLElement.prototype -> Element.prototype 
  -> Node.prototype -> Object.prototype -> null

You can access any of them and extend or inherit from as desired. But again, even though you can, I strongly advise not to.

When browser doesn’t expose these prototypes, you’re pretty much out of luck. You can try retrieving them by following constructor property of DOM element itself –

document.createElement('div').constructor;

– but then there’s no guarantee that element has constructor property (e.g. IE6 doesn’t) and even if it does, that this property references “correct” object. If, after all, constructor does reference correct object, there’s still no guarantee that this objects is allowed to be augmented at all. The truth is that host objects are allowed to implement completely bizarre behavior and do not even have to follow rules that native JS objects follow (you can find dozens of such examples in real life).

Second reason you want to avoid inheriting from DOM element prototypes is that mechanism of such inheritance is not really specified anywhere; it could be quirky, unpredictable and overall fragile and unreliable.

Yes, you can create a constructor that would initialize objects with proper prototype chain (i.e. having DOM prototype in it):

function MyDivElement(){}
MyDivElement.prototype = HTMLDivElement.prototype;

var myDiv = new MyDivElement();
typeof myDiv.appendChild; // "function"

– but this is as much as it goes, and usefulness of this whole approach becomes limited by having certain methods in prototype and nothing else –

typeof myDivElement.nodeName; // "undefined"
myDivElement.innerHTML = '<span>foo<\/span>';
myDivElement.childNodes; // Error

Until some standard specifies exact mechanism for inheriting from DOM prototypes (and browsers actually implement that mechanism), it’s best to leave them alone, and perhaps try alternative approach – e.g. wrapper or decorator patterns rather than prototype one 🙂

Leave a Comment