# node.traverse()

[🔰 JS](../../../../../js) ⟩ [browser](../../../..) ⟩ [DOM](../../..) ⟩ [types](../..) ⟩ [Node](..) ⟩ .traverse()

{% tabs %}
{% tab title="💾 程式" %}
* 💾 replit:  [node.traverse()](https://replit.com/@pegasusroe/nodetraverse#script.js)

```javascript
// depth-first traversal
// 🔸 node.traverse()
Node.prototype.traverse = function(f){
    // do something on this node
    f(this);
    // and on its child nodes
    for(let child of this.childNodes) {
        child.traverse(f);
    } 
};
```

💈範例：

```javascript
document.traverse(elem => console.log(elem.nodeName));
```
{% endtab %}

{% tab title="📗 參考" %}
* [ ] JavaScript: The Definitive Guide (15.3 Scripting Documents)
{% endtab %}

{% tab title="👥 相關" %}
* [traversing-dom](../../../traversing-dom "mention")
* [..](.. "mention")
{% endtab %}
{% endtabs %}
