我有一個 json 物件描述外部服務器上的檔案樹,當我嘗試遍歷物件的值以便我可以將樹呈現為 HTML 時,整個瀏覽器崩潰并在回圈開始時立即重新加載。這是我的代碼:
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);
});
物件本身不是格式錯誤的,我可以在將其發送到客戶端之前在服務器上對其進行迭代。我已經使用斷點通過除錯器驗證了崩潰正是location從第一次開始分配值的時間Object.values(fileTree)

uj5u.com熱心網友回復:
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; }
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/347642.html
標籤:javascript
