简体   繁体   English

在循环一次之前,Javascript 会在 for 循环开始时导致崩溃

[英]Javascript causes crash at start of for-loop before ever looping once

I have a json object that describes the file tree on an external server, when I try to iterate through the values of the object so that I can render the tree as HTML the entire browser crashes and reloads immediately at the start of the loop.我有一个 json 对象描述外部服务器上的文件树,当我尝试遍历对象的值以便我可以将树呈现为 HTML 时,整个浏览器崩溃并在循环开始时立即重新加载。 Here is my code:这是我的代码:

    HGEClient.send("queryfiletree").then(fileTree =>
    {
      let renderFolder = function(parentFolder, folder)
      {
        this.createCatelogFolder(parentFolder, folder);

        for(child of Object.values(folder.children))
          if(child.type == "folder") renderFolder(folder, child);
          else                       this.createCatelogFile(folder, child);
      }


      for(location of Object.values(fileTree)) // Crashes here
        if(location.type == "folder") renderFolder(null, location);
        else                          this.createCatelogFile(null, location);
    });

The object itself isn't ill-formed and I'm able to iterate on it on the server prior to sending it to the client.对象本身不是格式错误的,我可以在将其发送到客户端之前在服务器上对其进行迭代。 I have verified through the debugger using breakpoints that the crash is exactly when location is assigned a value the very first time from Object.values(fileTree)我已经使用断点通过调试器验证了崩溃正是在第一次从Object.values(fileTree)location分配值时

在此处输入图片说明

location also is a globally scoped object at client side, thus for the iteration location needs its own scope, preferably by being a let variable type, otherwise the script does break/crash. location也是客户端的全局范围对象,因此迭代location需要自己的范围,最好是let变量类型,否则脚本会中断/崩溃。

 const fileTree = [{ type: "folder", id: "foo", }, { type: "folder", id: "bar", }, { type: "file", id: "baz", }]; // `location` needs to be a variable type, preferably `let` // ---v for (let location of Object.values(fileTree)) { if (location.type === "folder") { console.log({ location }); } }
 .as-console-wrapper { min-height: 100%!important; top: 0; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM