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
Engineer at Fleek (YC W22). Building production multi-agent systems with LangGraph, MCP architecture, and AWS serverless. TypeScript | Node.js | Python.
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 😀
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)
These APIs are all very low-level and using them directly isn't all that practical. I usually prefer creating some simple wrappers around them, small and simple enough that I can just copy-paste them into any random project without having to worry about updating them.
For the HTMLElement class I've created this "improved" version that takes care of as much of the typical preamble without sacrificing too much flexibility or building a whole new framework.
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)
They are part of the JavaScript engine, no lower than an Array or any other API
A low-level programming language is a programming language that provides little or no abstraction from a computer's instruction set architecture—commands or functions in the language map that are structurally similar to processor's instructions. Generally, this refers to either machine code or assembly language.
Engineer at Fleek (YC W22). Building production multi-agent systems with LangGraph, MCP architecture, and AWS serverless. TypeScript | Node.js | Python.
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)
One thing nearly everyone does is due to incorrect documentation:
can be written as:
Note the use of
appendandappendChild. In most examplesappendChilds return value is never used.appendcan 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 usethisuntil it is created by thesuper()callThanks 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 😀
Shouldn't be innerHTMLing in constructor. Better to use the template
Can you motivate that statement, Benny
innerHTMLandappend(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
innerHTMLI don't know the guy personally .. but Justin Fagnani sounds familiar 😛
These APIs are all very low-level and using them directly isn't all that practical. I usually prefer creating some simple wrappers around them, small and simple enough that I can just copy-paste them into any random project without having to worry about updating them.
For the
HTMLElementclass I've created this "improved" version that takes care of as much of the typical preamble without sacrificing too much flexibility or building a whole new framework.Low-Level ??
They are part of the JavaScript engine, no lower than an Array or any other API
You can "improve" your code:
to
MDN append documentation
You should add
webcomponentstag to your article too.Thanks! will be adding it right away.
web components are losing steam? :O
Yes, they do.
They don't run on coal like Frameworks, they are part of JavaScripts' core technologies, thus run on modern power. No steam required any longer.