Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing JavaScript Web API Interfaces

HTMLElement (and it's descendants, such as HTMLDivElement, HTMLSpanElement etc) are defined as interfaces according to the MDN documentation.

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

class CustomElement implements HTMLElement {
    // implementation
}

However implementing interfaces in TypeScript doesn't generate any code, so it's difficult to see how to achieve interface implementation with pure JavaScript.

How should these interfaces be implemented with pure JavaScript?

like image 392
Matthew Layton Avatar asked Feb 26 '26 11:02

Matthew Layton


1 Answers

jsFiddle Demo

You would use the prototype of HTMLElement

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

And now you can construct them using the new keyword

var myElement = new CustomElement();
//myElement will now have access to the HTMLElement prototype
like image 192
Travis J Avatar answered Feb 28 '26 01:02

Travis J