递归函数中断
Recursive function break
下面的代码对一棵树进行 post 次遍历。目的是在特定条件下调用方法 returns false 时中断递归(参见下面的 _walkTree()
)。
function _walkPostOrder(tree, callback, ctx){
var continueWalk = true;
function _walk(tree, callback, ctx, parent){
for(var idx = 0, length = tree.length; idx < length; idx++){
console.log(continueWalk);
if(continueWalk) {
var node = tree[idx];
if(node.children && node.children.length > 0 && continueWalk)
_walk.call(this, node.children, callback, ctx, node);
continueWalk = callback.call(ctx, node, parent, tree, idx);
continue;
};
console.log(node);
break;
};
}
_walk(tree, callback, ctx);
}
var json = [{ text: "root", children: [
{id: "id_1", text: "node_1", children:[
{id: "id_c1", text: "node_c1"},
{id: "id_c2", text: "node_c2", children: [
{id: "id_c2_c1", text: "node_c2_c1"},
{id: "id_c2_c2", text: "node_c2_c2"},
{id: "id_c2_c3", text: "node_c2_c3"}]},
{id: "id_c3", text: "node_c3"}]},
{id: "id_2", text: "node_2"}]}];
//Iterate
(function _walkTree(){
_walkPostOrder.call(this, json, function(node, parentNode, siblings, idx){
console.log(node.id);
if(node.id == "id_c2_c2") return false;
return true;
}, this);
})();
我遇到的问题是,为什么 continueWalk
标志 returns 在被回调设置为 false
后返回到 true
。目的是它应该在那一点打破循环,以及上面递归函数的所有循环。
这个fiddle演示应该说清楚了:https://jsfiddle.net/xuxuq172/2/
您正在覆盖此处的 continueWalk
:
if(node.children && node.children.length > 0 && continueWalk)
_walk.call(this, node.children, callback, ctx, node);
continueWalk = callback.call(ctx, node, parent, tree, idx);
// ^^^^^^^^^^
您需要检查 continueWalk
的内容,因为之前调用了一行。
下面的代码对一棵树进行 post 次遍历。目的是在特定条件下调用方法 returns false 时中断递归(参见下面的 _walkTree()
)。
function _walkPostOrder(tree, callback, ctx){
var continueWalk = true;
function _walk(tree, callback, ctx, parent){
for(var idx = 0, length = tree.length; idx < length; idx++){
console.log(continueWalk);
if(continueWalk) {
var node = tree[idx];
if(node.children && node.children.length > 0 && continueWalk)
_walk.call(this, node.children, callback, ctx, node);
continueWalk = callback.call(ctx, node, parent, tree, idx);
continue;
};
console.log(node);
break;
};
}
_walk(tree, callback, ctx);
}
var json = [{ text: "root", children: [
{id: "id_1", text: "node_1", children:[
{id: "id_c1", text: "node_c1"},
{id: "id_c2", text: "node_c2", children: [
{id: "id_c2_c1", text: "node_c2_c1"},
{id: "id_c2_c2", text: "node_c2_c2"},
{id: "id_c2_c3", text: "node_c2_c3"}]},
{id: "id_c3", text: "node_c3"}]},
{id: "id_2", text: "node_2"}]}];
//Iterate
(function _walkTree(){
_walkPostOrder.call(this, json, function(node, parentNode, siblings, idx){
console.log(node.id);
if(node.id == "id_c2_c2") return false;
return true;
}, this);
})();
我遇到的问题是,为什么 continueWalk
标志 returns 在被回调设置为 false
后返回到 true
。目的是它应该在那一点打破循环,以及上面递归函数的所有循环。
这个fiddle演示应该说清楚了:https://jsfiddle.net/xuxuq172/2/
您正在覆盖此处的 continueWalk
:
if(node.children && node.children.length > 0 && continueWalk)
_walk.call(this, node.children, callback, ctx, node);
continueWalk = callback.call(ctx, node, parent, tree, idx);
// ^^^^^^^^^^
您需要检查 continueWalk
的内容,因为之前调用了一行。