Consider I want to extend the native button
element, and create my own super-button
element. As I know, it must follow the following pattern:
var SuperButton = document.registerElement('super-button', {
prototype: Object.create(HTMLButtonElement.prototype),
extends: 'button'
});
It looks strange to me - doesn't the prototype
and extends
parameters say the same thing? If I explicitly say that my super-button
use the HTMLButtonElement
prototype, why do I also need to specify that it extends the button element? isn't it redundant? For me it looks like exactly the same information.
Actually, its allows you to distinguish between Custom Tags declaration versus Type Extensions declaration (as suggested by Bergi's second comment), as both share the same method
document.registerElement()
.Type Extensions: use
extends
<button-v2>
will continue to act as a<button>
(ie keeps its semantics).Custom Tags: don't use
extends
<not-button>
inherits fromHTMLButtonElement
interface but lost its semantics (ie won't act as a<button>
)NB: If the only reason was really because you cannot always infer an
<element>
from its prototype chain, an optional parameter would have been proposed to disambiguate such rare cases. Here they have choosen a common, synthetic method, which is confusing at first sight you're right!To clarify Paulpro's answer: the
extends
creates theis=
syntax. Ifq
andblockquote
both shareHTMLQuoteElement
, and you extendq
, then you can write<q is="super-button">
but cannot write<blockquote is="super-button">
.Regarding what means to extend an
a
link with another element'sprototype
, this would create a link that does not have the appropriate functions and properties, which is bad:See: https://jsfiddle.net/3g0pus8r/
The result is:
However, you may still want to:
MyOtherAnchor
which in turn extendsHTMLAnchorElement
;HTMLElement
and implement all the anchor interface yourself.From the Custom Elements specification:
In other words, while it may be redundant for
<button>
elements, it isn't redundant in general and the spec needs to support the general case.I would argue that it isn't even redundant for
<button>
though, as there is nothing preventing you from doing: