admin管理员组文章数量:1334823
Maybe is a trivial problem, i don't know why this function exit from for cycle when it goes on else statement. I need this function to fetch an xml document.
function xmlToArray(element){
childs= element.childNodes;
if(childs.length != 1){
for(var i=0;i<childs.length;i++){
if(childs[i].hasChildNodes()){
xmlToArray(childs[i]);
}
alert("exit from if");
}//end for
alert("exit from for");
}//end if
else{
alert("do something with element");
}
alert("end of func");
}
Maybe is a trivial problem, i don't know why this function exit from for cycle when it goes on else statement. I need this function to fetch an xml document.
function xmlToArray(element){
childs= element.childNodes;
if(childs.length != 1){
for(var i=0;i<childs.length;i++){
if(childs[i].hasChildNodes()){
xmlToArray(childs[i]);
}
alert("exit from if");
}//end for
alert("exit from for");
}//end if
else{
alert("do something with element");
}
alert("end of func");
}
Share
Improve this question
edited Feb 26, 2010 at 17:37
Joel Coehoorn
416k114 gold badges578 silver badges813 bronze badges
asked Feb 26, 2010 at 17:28
Manuel BittoManuel Bitto
5,2636 gold badges42 silver badges48 bronze badges
1
- Do you mean it's dropping pletely from the stack, every iteration of it, when it encounters the else, or just the level it's on? – Tarka Commented Feb 26, 2010 at 17:30
1 Answer
Reset to default 8Since childs
is not a local variable, all calls of xmlToArray
work on the same data.
Try this:
function xmlToArray(element) {
var childs = element.childNodes;
// …
}
Using var
declares that variable in the current scope.
本文标签: recursionRecursive function in javascriptStack Overflow
版权声明:本文标题:recursion - Recursive function in javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742375236a2463029.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论