second+迭代后没有返回值
No return value after second+ iteration
我不知道这是怎么回事:
var get_depth = function(item, depth) {
if(item.parent_id !== null) {
get_depth(get_item_by_id(item.parent_id),depth+1);
} else {
alert ("return: " + depth);
return depth;
}
};
警报消息总是抛出正确的深度,但我希望在其中存储值的变量在两次或多次迭代后只接受一次迭代(返回值=1),我的变量的值未定义。我不明白。
您缺少返回。
var get_depth = function(item, depth) {
if(item.parent_id !== null) {
return get_depth(get_item_by_id(item.parent_id),depth+1);
} else {
alert ("return: " + depth);
return depth;
}
};
为什么不使用这种语法?我发现它更容易通过:
function get_depth(item, depth) {
if(item.parent_id !== null) {
return get_depth(get_item_by_id(item.parent_id),depth+1);
} else {
alert ("return: " + depth);
return depth;
}
}
这是因为每次都应该返回值:
var get_depth = function(item, depth) {
if(item.parent_id !== null) {
return get_depth(get_item_by_id(item.parent_id),depth+1);
} else {
alert ("return: " + depth);
return depth;
}
};
相关文章:
- 通过angularjs迭代多个字典中的键和值
- 如何在我传递给视图的ViewModel集合中获得jQuery可选对象的值,以作为有序列表进行迭代
- 从迭代器/生成器中获取单个yield值
- 尝试在两次迭代中警告同一选择器的值,在第一次迭代中得到正确的值,而在第二次迭代中获得不正确的值.为什么?
- 为什么数组在对象内部迭代对象时存储重复值
- 如何根据DB中的值迭代选择并设置多个选项
- text在迭代JavaScript XML中返回不需要的空文本值的内容
- 在AngularJS中使用ng repeat调用和迭代函数返回值
- second+迭代后没有返回值
- 为什么来自循环的警报总是返回最后一个值,而不是每个迭代值
- 基于mootools中数组的值迭代json对象
- 如何返回对象的所有属性及其值,包括可迭代的,自定义的和继承的
- 如何从for循环的每次迭代中返回值
- ES6中是否有收集迭代器的功能,包括最终返回值
- 解释为什么这个闭包在迭代时返回值
- JavaScript通过数组的赋值迭代到对象
- .map返回每次迭代的结果数组的长度
- 返回可迭代与 Resolve in Promise.all
- 尝试迭代"rem = (rem*value)%N"同时保存每个rem值为i+1,并在i= E时返回r
- JS函数迭代对象并只打印特定键的值返回undefined