从对象及其子对象中获取所有名称

Get all names from object and it's children object

我为这个任务苦苦挣扎了一段时间,决定需要你的帮助。如何让所有名称按行执行 (html head tag body h1 div...).

我使用递归方法写了一段代码,但某处失败了。执行“无法读取未定义的 属性 'forEach'”。能不能告诉我哪里错了,提示解决。

"use strict";

var treeString;

var Node = require("./Node");

 function TreeView(result){
     treeString = "";
 createTreeView(result, 0);
}

 var createTreeView = function (current, level) {
     treeString += current.children;

     current.children.forEach(function(childObj) {
         createTreeView(childObj, level + 1);
     });
};

TreeView.prototype.toString = function (){
   return treeString;
};
// Exports
module.exports = TreeView;

这应该迭代所有 dom 元素。粘贴到控制台,看看会发生什么。

var currentNode,
    ni = document.createNodeIterator(document.documentElement, NodeFilter.SHOW_ELEMENT);

while(currentNode = ni.nextNode()) {
    console.log(currentNode.nodeName);