JavaScript如何处理未知的价值? [英] How does JavaScript deals with value which is unknown?
问题描述
我是JavaScript的新手,正在阅读书,在递归章节中,有一个示例跟随
I am new to JavaScript and reading a book and in recursive chapter, there is an example as follow
function findSolution(target) {
function find(current, history) {
if (current == target)
return history;
else if (current > target)
return null;
else
return find(current + 5, "(" + history + " + 5)") ||
find(current * 3, "(" + history + " * 3)");
}
*return find(1, "1"); // when will this run?
}
console.log(findSolution(24));
// → (((1 * 3) + 5) * 3)
我无法理解JavaScript将如何运行这些原因,因为在所有三种 if
情况下,它期望 current
的值?但是作者只是运行函数的外部,该函数以 target
作为参数,而不是 current
作为参数.它是否采用
I am unable to understand how does JavaScript will run these causes as it expects a value of current
in all three if
cases? But the author is just running the outside of function which takes target
as an argument, not the current
. Does it take value of current and history from
return find(1,"1")
喜欢吊装吗?
推荐答案
current
和 history
是为 fund
上的参数指定的名称函数,而不是 findSolution
函数.
current
and history
are the names given to parameters on the fund
function, not the findSolution
function.
所以是的,当使用 find(1,"1")
调用该函数时,它将获得这些值(然后,当 find(current * 5,…)时,它将获得这些值.
和 find(current + 3, ...)
被调用.
So yes, it will get those values when find(1, "1")
is used to call that function (and then later, when find(current * 5, …)
and find(current + 3, …)
are called).
这在任何方面都不像起重或与起重有关.
This is not, in any way, like or related to hoisting.
这篇关于JavaScript如何处理未知的价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!