3 ms·
What’s weird about it? It’s the most obvious declarative way to build a hierarchy of objects in vanilla JS. It’s easy to read and straightforward to understand;
by tobr 6y ago
What’s weird about it? It’s the most obvious declarative way to build a hierarchy of objects in vanilla JS. It’s easy to read and straightforward to understand; if you want abstractions around it is completely obvious how you would go about building those yourself.
- weego 6y agorecreating html node declaration in JS with janky and verbose syntax for attributes and child nodes is not an answer to a problem anyone has.
- tobr 6y agoI guess I shouldn’t reply to flippant drive-by comments, but how the heck can that be considered verbose, and what’s your non-janky suggestion that doesn’t introduce some kind of extra tooling or parsing?
- blacktriangle 6y agoThe linked code doesn't show the full value of this technique, since its straightforward HTML that's coming out. Now what happens if you want a collection of list items generated by looping over an array? Assign classes based on some logical conditional? Conditional rendering? These are problems everybody has and every template language tries to solve with their own custom syntax. The point of this method is that rather than create some half-assed template language, you just write Javascript code that acts on and generates a data structure that can then be converted into HTML.
- pjmlp 6y agoWeb components and tagged strings are the most obvious way.
- tobr 6y agoTagged template literals can certainly be nice, but they basically require you to ship a parser to the browser, or introduce a complex build step, or give up the idea of doing vdom-like patched updates. (Not sure I see how web components fit into this discussion.)
- pjmlp 6y agoWeb components use shadow DOM for that, besides I rather let the browser engine do what it does best.