我有一個功能
function recursiveDivisionWrap(width, height, colStart, colEnd, rowStart, rowEnd)
其中我有一個空集
let blockedNodes = new Set();
在這個函式里面我有另一個遞回函式
function recursiveDivision(width, height, colStart, colEnd, rowStart, rowEnd)
每次呼叫recursiveDivision函式時,我都會通過添加值來更新一個blocksNodes集:
for (let i = colStart; i < colEnd; i ) {
blockedNodes.add(`c${i}r${wallStart}`);
}
如果我在recursiveDivision函式中設定了console.log blocksNodes,我會得到想要的結果,例如:
Set(43) {'c0r7', 'c1r7', 'c2r7', 'c3r7', 'c4r7', …}
但是,當我在recursiveDivisionWrap 中而不是在 recursiveDivision 中設定console.log blocksNodes時,我會得到逗號分隔的物件:
c0r7,c1r7,c2r7,c3r7,c4r7,c5r7,c6r7,c7r7,c8r7,c9r7,c9r0,c9r1,c9r2,c9r3,c9r4,c9r5,c9r6,c8r0,c8r1,c8r2,c8r3,c8r4,c8r5,c8r6,c3r0,c3r1,c3r2,c3r3,c3r4,c3r5,c3r6,c4r6,c5r6,c6r6,c7r6,c4r1,c5r1,c6r1,c7r1,c4r5,c5r5,c6r5,c7r5
我也試過陣列,結果是一樣的。
為什么它不回傳Set(43) {'c0r7', 'c1r7', 'c2r7', 'c3r7', 'c4r7', …}如果blockedNodes集外面定義recursiveDivision功能和內部recursiveDivisionWrap及為什么內部recursiveDivision函式回傳正確的設定?如果您能幫助我找到該問題的答案,我將不勝感激。
uj5u.com熱心網友回復:
此行為與進行輸出的位置無關,而是與輸出的格式有關。
你有這些陳述:
console.log(blockedNodes);
和:
console.log(`Is this a set? ${blockedNodes}`);
和:
let ecie = Array.from(blockedNodes);
console.log(`Is this an array? ${ecie}`)
他們不做同樣的事情。
console.log(blockedNodes)將渲染留給consoleAPI,它可能會提供附加資訊,例如物件的建構式的名稱(Set在本例中)、此集合具有的專案數等。它甚至可以添加用戶界面控制元件來展開/折疊集合的內容和潛在的嵌套資料結構。
console.log(`Is this a set? ${blockedNodes}`)將隱式呼叫blockedNodes.toString()以生成字串。除非toString已被覆寫,否則將呈現為[object Set],因此總輸出將是“這是一個集合嗎?[物件集]”。
console.log(`Is this an array? ${ecie}`)還將呼叫該toString方法,但這次 on ecie,它是一個陣列(因為這是Array.from回傳的內容)。toString陣列上的方法會生成一個逗號分隔的值串列,它解釋了您提到的輸出。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/338346.html
標籤:javascript 功能 目的 放
