Implementing JavaScript Web APIs

HTMLElement (and descendants such as HTMLDivElement , HTMLSpanElement , etc.) are defined as interfaces in accordance with the MDN Documentation .

If I wanted to implement these interfaces using TypeScript, I can do the following.

class CustomElement implements HTMLElement {
    // implementation
}

However, the implementation of interfaces in TypeScript does not generate any code, so it is difficult to understand how to achieve the implementation of an interface using pure JavaScript.

How should these interfaces be implemented using pure JavaScript?

+3
source share
1 answer

jsFiddle Demo

Would you use a prototype HTMLElement

var CustomElement = function(){}; 
CustomElement.prototype = HTMLElement.prototype;

And now you can build them using the keyword new

var myElement = new CustomElement();
//myElement will now have access to the HTMLElement prototype
+3
source

All Articles