You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Note that the entire tree structure can be derived from any node in the tree by visiting all of its parents and children. This means that one single node will suffice to describe the entire tree.
varhtml=newNode('html');varhead=newNode('head');head.append(newNode('title'));html.append(head);html.append(newNode('body'));for(varnodeofhtml.traverse())console.log(node.name);// html// head// title// body
The document object model (DOM) is essentially a tree (data structure), containing nodes with a number of optional attributes such as
id
orclass
.Each node in the tree can have one parent and several children and these properties connect the nodes together to form a tree.
Consider this boilerplate HTML (left) and its corresponding tree (right):
Note that the entire tree structure can be derived from any node in the tree by visiting all of its parents and children. This means that one single node will suffice to describe the entire tree.
So let's make a
Node
class:For inheritance to work we need to extend
Node
with a method that allows us to append child nodes:Neat! We just learned how to make a circular object. How do we traverse it? With generators, we can do it recursively in a few lines of code:
Finally, we can add a simple rendering method:
The text was updated successfully, but these errors were encountered: