Creating a Custom Element Using Anonymous Class

javascript
Published on February 3, 2021

While creating a new custom element, it is not necessary to give a name to the defining class. The custom element can be created using an anonymous class also.

For example, consider the below code where a custom element <input-plus-minus> is created using a class name InputPlusMinus.

class InputPlusMinus extends HTMLElement {
	constructor() {
		super();

		// rest of the Javascript
	}
}

customElements.define('input-plus-minus', InputPlusMinus);

The same thing can be done without explicitly giving a class name by using an anonymous class.

// defining custom element using anonymous class
customElements.define('input-plus-minus', class extends HTMLElement {
	constructor() {
		super();

		// rest of the Javascript
	}
});
In this Tutorial