Online since 1990 Yes! I started with Gopher. I do modern Web Component Development with technologies supported by **all** WHATWG partners (Apple, Google, Microsoft & Mozilla)
constructor(){super()// sets and returns 'this'.attachShadow({mode:"open"})//sets and return this.shadowRoot.append(template.content.cloneNode(true));}
Note the use of append and appendChild. In most examples appendChilds return value is never used. append can added multiple text-nodes or elements.
And the global template.innerHTML isn't necessary either:
constructor(){super().attachShadow({mode:"open"}).innerHTML=` ... any HTML here... `;}
Docs also say you "use super() first in constructor"
That is incorrect also.
You can use any JavaScript beforesuper(); you just can not use this until it is created by the super() call
Coding is as much a matter of personal growth as it is of logic and control-flow. I keep patience, curiosity, & exuberance in the same toolbox as vim and git.
*Opinions posted are my own*
Online since 1990 Yes! I started with Gopher. I do modern Web Component Development with technologies supported by **all** WHATWG partners (Apple, Google, Microsoft & Mozilla)
Coding is as much a matter of personal growth as it is of logic and control-flow. I keep patience, curiosity, & exuberance in the same toolbox as vim and git.
*Opinions posted are my own*
Online since 1990 Yes! I started with Gopher. I do modern Web Component Development with technologies supported by **all** WHATWG partners (Apple, Google, Microsoft & Mozilla)
Thanks for letting us know about this. The code looks much cleaner, and yes! not creating a template element would be much better as the global namespace wouldn't be polluted. I'll add this comment to the article 😀
Truly said. Altough I wouldn't say just because of this, that the docs are "incorrect". Maybe the first one is more readable for newcomers. Anyway, super useful comment though. Thanks.
Online since 1990 Yes! I started with Gopher. I do modern Web Component Development with technologies supported by **all** WHATWG partners (Apple, Google, Microsoft & Mozilla)
If you want to score W3C points; feel free to update that MDN article.
I did some MDN updates; then concluded there is way more effect adding comments to Web Component blogposts.
And I recently published my first Dev.to post explaining Web Components are about semantic HTML (IMHO)
And ... there is more wrong in that MDN Count Words example..
this.parentNode DOM access should be done in the connectedCallback, as the DOM might not even exist when the constructor runs
<p is="word-count"></p> Will never work in Safari, because Apple only implemented Autonomous Elements and (for the past 5 years) has strong arguments to not implement Customized Built-In Elements
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
One thing nearly everyone does is due to incorrect documentation:
can be written as:
Note the use of
append
andappendChild
. In most examplesappendChild
s return value is never used.append
can added multiple text-nodes or elements.And the global template.innerHTML isn't necessary either:
Docs also say you "use super() first in constructor"
That is incorrect also.
You can use any JavaScript before
super()
; you just can not usethis
until it is created by thesuper()
callShouldn't be innerHTMLing in constructor. Better to use the template
Can you motivate that statement, Benny
innerHTML
andappend(Child)
all do the same: they add content to a DOMThat
.createElement"template")
takes extra CPU cycles; content is parsed once.With
.innerHTML=
content is also parsed once.So there is only benefit if code does
template.cloneNode(true)
multiple timesif you know you're only going to use the element once, yeah there's no difference.
But if you're going to construct the element several times, then you're going to invoke the parser on each instance.
If the element's shadow DOM is extensive, that could add up fast.
Moreover, keeping the template static means it's parsed up front.
If you don't take my word; then maybe you take this guy his comment on
innerHTML
I don't know the guy personally .. but Justin Fagnani sounds familiar 😛
Thanks for letting us know about this. The code looks much cleaner, and yes! not creating a template element would be much better as the global namespace wouldn't be polluted. I'll add this comment to the article 😀
Truly said. Altough I wouldn't say just because of this, that the docs are "incorrect". Maybe the first one is more readable for newcomers. Anyway, super useful comment though. Thanks.
See what code newcomers have to dig through (unless they don't RTFM) in the
attachShadow
MDN documentation: developer.mozilla.org/en-US/docs/W...If you understand "// Always call super first in constructor" is incorrect
you can write that 33 lines
constructor
(most likely) without any comments:If you want to score W3C points; feel free to update that MDN article.
I did some MDN updates; then concluded there is way more effect adding comments to Web Component blogposts.
And I recently published my first Dev.to post explaining Web Components are about semantic HTML (IMHO)
And ... there is more wrong in that MDN Count Words example..
this.parentNode
DOM access should be done in theconnectedCallback
, as the DOM might not even exist when theconstructor
runs<p is="word-count"></p>
Will never work in Safari, because Apple only implemented Autonomous Elements and (for the past 5 years) has strong arguments to not implement Customized Built-In Elements