Okay, so now we have a little bit of a picture of what is a DOM? Let's see what is a virtual DOM. And it's actually what the name suggests. It's a virtual representation of the actual DOM. So this could be, for example, JavaScript objects, which is exactly what we are going to do today. And this thing lives in the browser, or in the memory of the browser, and it is synchronized with the actual DOM. And this is what Vue.js uses under the hood, and also some other frameworks. But for example, Petite View doesn't use a virtual DOM, which is fine.
Now, you might ask yourself, OK, but some use it, some don't use it, but why should we use it, or why do we have a virtual DOM. Especially in the morning we heard Avenue say it has a lot of performance overhead, or some performance overhead. But let's take a look. So the great thing here is, it decouples the rendering logic from the actual DOM, and it's easier to programmatically manipulate and inspect the whole thing if we want to do something custom. It's also easier to reuse outside of the browser, for example, if you do a native mobile app, or, for example, a WebGL renderer. I heard Avenue once answer a question in an interview where he said that someone built a, based on the Vue 3 virtual DOM, a WebGL renderer, which is pretty cool.
All right, before we start building this thing, because we take a look at the actual DOM, what is it that we need to build here? So let's take a look at this very simple HTML markup, and what do we have here? So we have these things that we call tags, right? So we need to represent tags in our virtual DOM. So let's just write it down. We need tags. What else do we need here? These are attributes. It could also be a function, like an onClick event or something like that. So let's just call this properties, and then we have the last part, which is our content, but we're not going to call it content because it's a tree, so it's just the tree has children. We just call it children. But as you can see in this example, children isn't the most intuitive way of putting it because it could be just text, it could be just one item in there, or it could be various children. So in the last part it makes most sense to call it children, but this is how it's called.
Alright, so to create nodes we have this H function or Hyperscript, which means just JavaScript that produces HTML. So let's take a look at what we can create with this. So I represent here three different things we can do. Here you can see in the third parameter, or the third argument here, it's a text. So our virtual DOM, our DOM can have a text. What else can it have? It can have another virtual node which then has, in this case, another text. But it could be until, like, it can have as many children as necessary, or as you want. And then we have the third example, which is you have an array of these nodes.
Comments