Skip to content

Instantly share code, notes, and snippets.

@cassiorsfreitas
Last active June 30, 2024 20:22
Show Gist options
  • Save cassiorsfreitas/51fe2bfac8ec89a8b21a53fb0ab87c9d to your computer and use it in GitHub Desktop.
Save cassiorsfreitas/51fe2bfac8ec89a8b21a53fb0ab87c9d to your computer and use it in GitHub Desktop.
Conceptual Aside: POJOs
// aside concepts: recursion, declarative/imperative programming, "virtual" dom, and "fake" react engine.
let markup = {
type: "article",
children: [
{
type: "h2",
children: [
{
type: "text",
value: "Counter"
}
]
},
{
type: "p",
children: [
{
type: "text",
value: "Counter"
},
{
type: "strong",
children: [
{
type: "em",
children: [
{
type: "text",
value: " 1"
}
]
}
]
},
{
type: "text",
value: " times"
}
]
},
{
type: "button",
children: [
{
type: "text",
value: "Click me"
}
]
}
],
};
const main = document.getElementById("app");
function addElements(pojoElement, parentDOMNode) {
let newDOMNode = pojoElement.type === "text"
? document.createTextNode(pojoElement.value)
: document.createElement(pojoElement.type);
if (pojoElement.children) {
pojoElement.children.forEach(child => {
addElements(child, newDOMNode);
});
}
parentDOMNode.appendChild(newDOMNode);
}
addElements(markup, main);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment