3

I am trying to extend the DOM. I "subclassed" from the Div element:

var Subclass = function() {}
Subclass.prototype = document.createElement('div');
Subclass.constructor = Subclass;

var obj = new Subclass();
var obj.innerHTML = 'test';
document.body.appendChild(obj); // Exception: Node cannot be inserted ... point in the hierarchy

So if an object's prototype being a DOM object won't do, what is the requirement for an object to be inserted into the DOM?

Oliver Zheng
  • 7,831
  • 8
  • 53
  • 59

3 Answers3

0

This actually works in Firefox.. Sort of, anyway. It won't actually create new elements when doing new Subclass, but keeps using the same.

In any case, I would say the DOM objects themselves are "special" in the way that only they can be appendChild'd.

Jani Hartikainen
  • 42,745
  • 10
  • 68
  • 86
0

According to W3C specification appendChild():

  1. Returns Node.
  2. Throws DOMException.
  3. Requires one argument of type Node.

From specification:

Node appendChild(in Node newChild) raises(DOMException);
Crozin
  • 43,890
  • 13
  • 88
  • 135
0

if you want to extend only div elements then may be you can do that by

HTMLDivElement.prototype.newFunction = function () { //'this' will get that div dom }

then it will extend only the div elements.. works on FF, don't know about other browsers.

hope this may help you.