這裏是代碼片斷,這進入了一個無限循環 - 「太多的遞歸錯誤」。Javascript簡單遞歸問題
SW = {
WData : {
wf : {
roots : [852,1517,1523,1540],
leaves : [],
features : {
852: { "cf":"855,1848"},
1517: { "cf":"1929,1930"},
1523: { "cf":""},
1540: { "cf":"1546,1549"},
855: { "cf":"" },
1848: { "cf":""},
1929: { "cf":""},
1930: { "cf":""},
1546: { "cf":"1600"},
1549: { "cf":""},
1600: { "cf":""}
}
}
},
init: function init(){
this.buildTree();
//console.log(this.WData.wf.leaves);
},
buildTree : function(){
this.getLeaves(this.WData.wf.roots);
},
getLeaves: function(roots){
for(var i in roots){
var root = this.WData.wf.roots[i];
if(this.WData.wf.features[ root ].cf === ""){
this.WData.wf.leaves.push(root);
return false;
}
else{
this.getLeaves(this.WData.wf.features[root].cf.split(',').map(Number));
}
}
return false;
}
}
SW.init();
無法理解此處存在的問題。我有一種感覺,我犯了一個很簡單的錯誤。