I am experimenting with this a bit. The big difficulty is that you need a document context to create an element. You can go with window.document by default, but it's boring.
Here is the POC I'm working on:
function CustomNode(type, parent) { if (type instanceof Node) { // Decorate a preexisting node appropriately if called that way. if (arguments.length === 2 && type.ownerDocument !== parent) { // Import the node if it not owned by the requested document. type = parent.importNode(type, true); } return Object.assign(type, this.__proto__); } //Normal flow, eg, new CustomNode("div"); var d = document; if (parent) { // Alt document flow, eg, new CustomNode("div", xmlDoc); if (parent.nodeType === 9) { d = parent; } else { // Support for new CustomNode("div", parentElement); // This doesn't append the element, just makes sure // the documents match d = parent.ownerDocument; } } var inst; // Creation flags if (type[0] === '
Reason, this is probably a bad idea: all in the same scope of these functions (including the elements themselves) will never receive garbage collection, since GC in most browsers is dead-dumb when it comes to elements referencing objects. I will never use it for production just for this reason: this is a direct memory leak.
Fordi
source share