我有這樣的函式和全域變數(作為陣列):
const arraysList = []
export const changeColorCategories = (array, draggedColumnId) => {
const isColor = arraysList.length ? arraysList[0][0]?.color : [];
if (typeof isColor === 'string') {
firstLevelColor = isColor;
}
return array.map((item, index, categories) => {
item.color = draggedColumnId !== 3 ? '#010172' : '#000000';
arraysList.push(categories);
if (firstLevelColor && !draggedColumnId) {
item.color = firstLevelColor;
}
if (item?.children?.length) {
changeColorCategories(item.children);
}
return item;
})
}
每次呼叫此函式都會將一些資料推送到陣列中。在這個函式中,我使用遞回。那么只有當這個函式結束它的作業時,我才能清除這個陣列。
uj5u.com熱心網友回復:
你可以在另一個函式中呼叫遞回函式,這樣你就可以在函式結束時運行任何你想要的東西
const arraysList = []
export const changeColorCategories = (array, draggedColumnId) => {
const isColor = arraysList.length ? arraysList[0][0]?.color : [];
if (typeof isColor === 'string') {
firstLevelColor = isColor;
}
return array.map((item, index, categories) => {
item.color = draggedColumnId !== 3 ? '#010172' : '#000000';
arraysList.push(categories);
if (firstLevelColor && !draggedColumnId) {
item.color = firstLevelColor;
}
if (item?.children?.length) {
changeColorCategories(item.children);
}
return item;
})
}
function runRucFunc(){
const result = changeColorCategories();
//Your other code goes here
return result;
}
uj5u.com熱心網友回復:
您可以將遞回部分放在子函式中。
下面我呼叫了內部函式inner
,我也將arrayList移到了函式中,由于你纏繞的閉包甚至不需要清除arrayList,它會在超出范圍時自動清除。
例如。
export const changeColorCategories = (array, draggedColumnId) => {
const arraysList = []
function inner(array, draggedColumnId) {
const isColor = arraysList.length ? arraysList[0][0]?.color : [];
if (typeof isColor === 'string') {
firstLevelColor = isColor;
}
return array.map((item, index, categories) => {
item.color = draggedColumnId !== 3 ? '#010172' : '#000000';
arraysList.push(categories);
if (firstLevelColor && !draggedColumnId) {
item.color = firstLevelColor;
}
if (item?.children?.length) {
inner(item.children); //we call inner here instead.
}
return item;
})
}
// now call our inner
// you could do something even before your recursion.
const result = inner(array, draggedColumnId);
// here we can put what we want after recursion.
return result;
}
uj5u.com熱心網友回復:
您可以將遞回呼叫包裝在另一個函式中,如下所示:
const arr = []
const recursive = (counter = 0) => {
if(counter === 5)
return arr.map((v) => String.fromCodePoint(65 v))
arr.push(counter)
return recursive( counter)
}
const go = () => {
console.log(recursive()) // [A,B,C,D,E]
console.log(arr) // [0,1,2,3,4]
arr.length = 0 // clear the array
console.log(arr) // []
}
go()
或者,如果全域陣列實際上不需要是全域的,而僅僅是遞回演算法作業資訊的容器,那么您可以將其作為遞回函式的引數,然后它將超出范圍(并且是垃圾收集)當遞回結束時。
const recursive = (counter = 0, arr = []) => {
if(counter === 5)
return arr.map((v) => String.fromCodePoint(65 v))
arr.push(counter)
return recursive( counter, arr)
}
console.log(recursive()) // [A,B,C,D,E]
console.log(arr) // Error! Not in scope!
go()
或者,您可以使遞回函式更智能,并且能夠檢測到它何時處理最終遞回:如何完成將取決于遞回函式的精確邏輯。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/487931.html
標籤:javascript 数组 功能 变量 ecmascript-6
上一篇:通用物件屬性