我工作在递归函数上:

function Recursive(JSObject) {

    for(let JSChild of JSObject.children) {
        console.log(JSChild.name);
        console.log(JSChild.hasChildNodes);
        /*if(JSChild.hasChildNodes) {
            Recursive(JSChild);
        }*/
    }
}


不幸的是,这条线:


  console.log(JSChild.hasChildNodes);


总是返回“未定义”,而某些节点有一个或多个子代。
此外,w3school在hasChildNodes的末尾使用一些括号,但是当我这样做时,我的浏览器(Chrome)返回此错误:


  JSChild.hasChildNodes不是一个函数


我不明白为什么什么都不起作用。
如果您能帮助我,我将不胜感激。

编辑:(JSON格式的对象)

{
  "dir":"",
  "name":"myProject",
  "type":"folder",
  "children":[
    {
      "dir":"myProject",
      "name":"css",
      "type":"folder",
      "children":[
        {
          "dir":"myProject/css",
          "name":"main.css",
          "type":"file"
        },
        {
          "dir":"myProject/css",
          "name":"menu.css",
          "type":"file"
        },
        {
          "dir":"myProject/css",
          "name":"user.css",
          "type":"file"
        }
      ]
    },
    {
      "dir":"myProject",
      "name":"fonts",
      "type":"folder"
    },
    {
      "dir":"myProject",
      "name":"images",
      "type":"folder",
      "children":[
        {
          "dir":"myProject/images",
          "name":"logo.png",
          "type":"file",
          "corrupted":true
        }
      ]
    },
    {
      "dir":"myProject",
      "name":"index.html",
      "type":"file"
    },
    {
      "dir":"myProject",
      "name":"js",
      "type":"folder",
      "children":[
        {
          "dir":"myProject/js",
          "name":"controllers",
          "type":"folder",
          "children":[
            {
              "dir":"myProject/js/controllers",
              "name":"core",
              "type":"folder",
              "children":[
                {
                  "dir":"myProject/tempjs/controllerslates/core",
                  "name":"menu.js",
                  "type":"file"
                }
              ]
            },
            {
              "dir":"myProject/js/controllers",
              "name":"errors",
              "type":"folder",
              "children":[
                {
                  "dir":"myProject/js/controllers/errors",
                  "name":"error.js",
                  "type":"file"
                }
              ]
            },
            {
              "dir":"myProject/js/controllers",
              "name":"home",
              "type":"folder",
              "children":[
                {
                  "dir":"myProject/js/controllers/home",
                  "name":"homePage.js",
                  "type":"file"
                }
              ]
            },
            {
              "dir":"myProject/js/controllers",
              "name":"home",
              "type":"folder",
              "children":[
                {
                  "dir":"myProject/js/controllers/user",
                  "name":"list.js",
                  "type":"file"
                },
                {
                  "dir":"myProject/js/controllers/user",
                  "name":"login.js",
                  "type":"file"
                },
                {
                  "dir":"myProject/js/controllers/user",
                  "name":"profile.js",
                  "type":"file"
                },
                {
                  "dir":"myProject/js/controllers/user",
                  "name":"subscribe.js",
                  "type":"file"
                }
              ]
            }
          ]
        },
        {
          "dir":"myProject/js",
          "name":"libs",
          "type":"folder",
          "children":[
            {
              "dir":"myProject/js/libs",
              "name":"handlebars.min.js",
              "type":"file"
            },
            {
              "dir":"myProject/js/libs",
              "name":"jquery.min.js",
              "type":"file"
            },
            {
              "dir":"myProject/js/libs",
              "name":"require.min.js",
              "type":"file",
              "corrupted":true
            }
          ]
        },
        {
          "dir":"myProject/js",
          "name":"main.js",
          "type":"file"
        },
        {
          "dir":"myProject/js",
          "name":"models",
          "type":"folder",
          "children":[
            {
              "dir":"myProject/js/models",
              "name":"menu.js",
              "type":"file"
            },
            {
              "dir":"myProject/js/models",
              "name":"user.js",
              "type":"file"
            },
            {
              "dir":"myProject/js/models",
              "name":"users.js",
              "type":"file"
            }
          ]
        }
      ]
    },
    {
      "dir":"myProject",
      "name":"ressources",
      "type":"folder"
    },
    {
      "dir":"myProject",
      "name":"views",
      "type":"folder",
      "children":[
        {
          "dir":"myProject/templates",
          "name":"core",
          "type":"folder",
          "children":[
            {
              "dir":"myProject/templates/core",
              "name":"footer.html",
              "type":"file"
            },
            {
              "dir":"myProject/templates/core",
              "name":"header.html",
              "type":"file"
            },
            {
              "dir":"myProject/templates/core",
              "name":"menu.html",
              "type":"file"
            }
          ]
        },
        {
          "dir":"myProject/templates",
          "name":"errors",
          "type":"folder",
          "children":[
            {
              "dir":"myProject/templates/errors",
              "name":"error401.html",
              "type":"file"
            },
            {
              "dir":"myProject/templates/errors",
              "name":"error403.html",
              "type":"file"
            },
            {
              "dir":"myProject/templates/errors",
              "name":"error404.html",
              "type":"file",
              "corrupted":true
            }
          ]
        },
        {
          "dir":"myProject/templates",
          "name":"home",
          "type":"folder",
          "children":[
            {
              "dir":"myProject/templates/home",
              "name":"homePage.html",
              "type":"file"
            }
          ]
        },
        {
          "dir":"myProject/templates",
          "name":"home",
          "type":"folder",
          "children":[
            {
              "dir":"myProject/templates/user",
              "name":"list.html",
              "type":"file"
            },
            {
              "dir":"myProject/templates/user",
              "name":"login.html",
              "type":"file"
            },
            {
              "dir":"myProject/templates/user",
              "name":"profile.html",
              "type":"file"
            },
            {
              "dir":"myProject/templates/user",
              "name":"subscribe.html",
              "type":"file"
            }
          ]
        }
      ]
    }
  ]
}

最佳答案

Object没有名为hasChildNodes的属性或方法,它是Node的方法。

09-25 20:12