Last active
June 30, 2024 20:22
-
-
Save cassiorsfreitas/51fe2bfac8ec89a8b21a53fb0ab87c9d to your computer and use it in GitHub Desktop.
Conceptual Aside: POJOs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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