简体   繁体   English

JavaScript 递归 function 在分配给变量时如何工作?

[英]how JavaScript recursive function works when assigned to variable?

Hi I am new to JavaScript but i don't understand how recursive function works when assigned to a variable.嗨,我是 JavaScript 的新手,但我不明白递归 function 在分配给变量时是如何工作的。 Below code should have only two outputs but instead it has 3 output.下面的代码应该只有两个输出,但它有 3 个 output。

 function capitalizeFirst(array) { if (array.length === 1) { return [array[0][0].toUpperCase() + array[0].substr(1)]; } const res = capitalizeFirst(array.slice(0, -1)); console.log(res); return res; } console.log(capitalizeFirst(['car', 'taco', 'banana']));

As I think: first capitalizeFirst(array.slice(0, -1));正如我所想:首先 capitalizeFirst(array.slice(0, -1)); will work as recursive function and return ['Car'] as result to res, then console.log(res) print result then return statement return res to function so final console.log(capitalizeFirst(['car', 'taco', 'banana']));将作为递归 function 返回 ['Car'] 作为 res 的结果,然后 console.log(res) 打印结果然后返回语句返回 res 到 function 所以最终 console.log(capitalizeFirst(['car', 'taco', '香蕉'])); should return ['car'] as a result.结果应该返回 ['car'] 。 So in console screen I should get two output but instead I am getting three why?所以在控制台屏幕上我应该得到两个 output 但我却得到三个,为什么?

TLDR : Your recursive function outputs the first element of the array as a capitalized word equal to the length of your array - 1 AND this result is outputted. TLDR :您的递归 function 将数组的第一个元素输出为等于数组长度的大写单词 - 1 并输出此结果。 The total outputs being equal to the array length.总输出等于数组长度。

Your array has a length of 3 and in your base case will return an array.您的数组长度为 3,在您的基本情况下将返回一个数组。 So res will be outputted 2 times within the recursive function and once outside.因此 res 将在递归 function 内输出 2 次,在外部输出 1 次。

If you have an array that is just [Car] , then the recursive function will return [Car] which will be outputted.如果您的数组只是[Car] ,则递归 function 将返回[Car]并将被输出。

If you have an array that is [car, taco] , it will first go through the recursive function, then shrink down to [Car] and since this is the base case, will return [Car] .如果你有一个数组[car, taco] ,它将首先通过递归 go function 缩小到[Car]并且由于这是基本情况,将返回[Car] This means that within the recursive function, res = [Car] .这意味着在递归 function 中, res = [Car] And since you have console.log(res) and return res , [Car] will be outputted within the recursive function and then outputted outside.并且由于您有console.log(res)return res[Car]将在递归 function 内输出,然后输出到外部。

In the scenario with an array length of 3, it will output [Car] twice within since the recursive function continues until the length is 1 (Meaning that it will hit the console.log twice).在数组长度为 3 的情况下,它将在 output [Car]内两次,因为递归 function 一直持续到长度为 1(这意味着它将两次访问console.log )。 And finally when res is returned, it is [Car] which will be outputted by the outside console.log .最后返回 res 的时候,就是[Car]会被外面的console.log输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Javascript日期可用于临时对象,但在首次分配给变量时会失败 - Javascript Date works with temporaries but fails when first assigned to a variable 闭包仅在分配给新变量时有效-Javascript - Closure only works when assigned to new variable - Javascript 了解递归函数如何在javascript中工作 - Understanding how a recursive function works in javascript 如何使用被调用的函数分配变量它是如何工作的 - How a variable can be assigned with a function that's is invoked how does that works 用var分配给变量的Javascript函数 - Javascript function assigned to variable with var 如何知道何时将新值分配给回调函数(JavaScript)中的全局变量 - How to know when the new value is assigned to global variable in callback function (JavaScript) 只有将变量分配给函数时,JavaScript才起作用? - Javascript only works when assigning variable to function? 分配给变量时函数未定义 - Function undefined when assigned to variable 如何选择在函数中将变量分配给哪个参数? -Javascript - How to choose what argument a variable is assigned to in a function? -Javascript 如何防止javascript函数评估分配给字符串变量的参数? - How to prevent a javascript function from evaluating an argument assigned to a string variable?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM