JavaScript 递归
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5164039/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JavaScript Recursion
提问by mjmitche
Would someone be kind enough to explain this program (taken from a book tutorial) step by step in plain language to help me understand recursion?
有人愿意用通俗的语言逐步解释这个程序(取自一本书的教程)以帮助我理解递归吗?
var reverseArray = function(x,indx,str) {
return indx == 0 ? str : reverseArray(x, --indx, (str+= " " + x[indx]));;
}
var arr = new Array('apple', 'orange', 'peach', 'lime');
var str = reverseArray(arr,arr.length,"");
alert(str);
var arr2 = ['car','boat','sun','computer'];
str = reverseArray(arr2,arr2.length."");
alert(str);
回答by kojiro
Sure, but it's probably easier to read written like this:
当然,但像这样写可能更容易阅读:
var reverseArray = function(x,indx,str) {
if (indx === 0) { // Termination condition
return str; // return default
} else {
return reverseArray(x, --indx, str + " " + x[indx]); // recur on x, reduce indx, update default
}
}
Recursion is just a function calling itself, right? The important thing is the termination condition that prevents the function from calling itself forever. In this case that's this line:
递归只是一个调用自身的函数,对吗?重要的是阻止函数永远调用自身的终止条件。在这种情况下,这是这一行:
if (indx === 0)…
As long as indx is not 0, the function will continue to call itself with updated arguments. The subsequent call does the same thing, but the final product strcontains the value passed from the parent call to reverseArray. Eventually indx reaches zero and the value of str is the combined value passed down from parent to child. That is what is returned by the line:
只要 indx 不为 0,该函数将继续使用更新的参数调用自身。后续调用执行相同的操作,但最终产品str包含从父调用传递给 的值reverseArray。最终 indx 达到零并且 str 的值是从父级传递给子级的组合值。这就是该行返回的内容:
return str; // ' lime peach orange apple'
It gets returned to its parent, and its parent returns that to its parent and so on until the top-most frame is reached.
它被返回给它的父级,它的父级将它返回给它的父级,依此类推,直到到达最顶层的帧。
arr = new Array('apple', 'orange', 'peach', 'lime');
reverseArray(arr,arr.length,""); // ['apple', 'orange', 'peach', 'lime'], 4, ""
+['apple', 'orange', 'peach', 'lime'], 3, "" + " " + x[3] = " lime";
++['apple', 'orange', 'peach', 'lime'], 2, " lime" + " " + x[2] = " lime peach";
+++['apple', 'orange', 'peach', 'lime'], 1, " lime peach" + " " + x[1] = " lime peach orange";
++++['apple', 'orange', 'peach', 'lime'], 0, " lime peach orange" + " " + x[0] = " lime peach orange apple"; // indx == 0, so this string returns
回答by FarligOpptreden
Would the following suffice?
以下就够了吗?
// declare a variable which is to be the function for reversing the array
var reverseArray = function(x,indx,str) {
// check if the index has reached zero. if it did, return the concatenated string,
// else concatenate the string with the next item in the array at the current index.
// for each subsequent call to the function, the index is decreased by one, working
// the array backwards.
return indx == 0 ? str : reverseArray(x, --indx, (str+= " " + x[indx]));;
}
// declare an array of fruit
var arr = new Array('apple', 'orange', 'peach', 'lime');
// declare a variable and assign it's value to the result of the recursive function,
// sending through the fruit array, the amount of items in it and an empty string as
// parameters to the function.
var str = reverseArray(arr,arr.length,"");
// show a dialogue box with the result of the function
alert(str);
// do the same as in the fruit example...
var arr2 = ['car','boat','sun','computer'];
str = reverseArray(arr2,arr2.length."");
alert(str);
回答by Krishna
One of the easiest ways to look at it is, think of it as calling another function having the same logic. Each function calls another function until a termination condition is met, in this case indx==0. At that point, it stops calling another function and returns str.
查看它的最简单方法之一是,将其视为调用具有相同逻辑的另一个函数。每个函数调用另一个函数直到满足终止条件,在这种情况下,indx==0。此时,它停止调用另一个函数并返回 str。

