Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What advantages provide document.registerElement()?

Using own custom tag directly in HTML document without using document.registerElement('my-tag') seems to works fine. It can be styled as usual etc.

like image 328
farincz Avatar asked Aug 29 '15 11:08

farincz


1 Answers

registerElement has two advantages:

  1. It returns a constructor for that tag, meaning you can instantiate it via Javascript, e.g:
    var myTag = document.registerElement("my-tag"); document.body.appendChild(new myTag())
  2. It provides a second parameter which allows you to select a prototype for your element, e.g.
    var customImg = document.registerElement("custom-img", { prototype: Object.create(HTMLImageElement.prototype) });

The second one is actually a general options object where an additional prototype can be extended or an existing tag be extended (like <img is="custom-img">)

like image 67
CodingIntrigue Avatar answered Oct 14 '22 16:10

CodingIntrigue