简体   繁体   English

为什么返回未定义但console.log返回一个int?

[英]Why would a return be undefined but console.log return an int?

So I have the following function: 所以我有以下功能:

var multiplyT = function(a, b, acc) {

    if (b == 0) {
        console.log("BASE CASE: ", acc);

        return acc;
    } else {
        b--;
        acc = acc + a;
        console.log("NOT THE BASE CASE: ", a,b,acc);
        multiplyT(a, b, acc);
    }

}

It gets called with: 它被调用:

console.log(multiplyT(5,3,0));

And gives this: 并给出这个:

NOT THE BASE CASE:  5 2 5
NOT THE BASE CASE:  5 1 10
NOT THE BASE CASE:  5 0 15
BASE CASE:  15
undefined

As output. 作为输出。 What I am confused about is why acc would give the correct value for the console.log but be "undefined" according to what is returned. 令我困惑的是为什么acc会为console.log提供正确的值,但根据返回的内容为“undefined”。

在你的else块中,它应该是return multiplyT(a, b, acc);

This is a good one. 这个不错。 Recursion can make your head spin. 递归可以使你的头旋转。 The reason it's undefined is because not all iterations return a value, and for those that don't you get undefined -- the same as if you set a variable to any function that doesn't return a value. 它未定义的原因是因为并非所有迭代都返回一个值,而对于那些没有返回值的那些,你会得到未定义的 - 就像你将一个变量设置为任何不返回值的函数一样。

It gets confusing with recursion though, because the return value you're seeing in this case is from the first-called and last-completed iteration. 然而,它会因递归而变得混乱,因为在这种情况下您看到的返回值来自第一次调用和最后完成的迭代。 Unlike a regular method call where return breaks execution of the method -- sending it back to from wherever it came, recursion still has wind its way back through the call stack, returning whatever values it has to give back, including undefined, in the reverse order in which they were called. 与常规方法调用不同的是,返回会中断方法的执行 - 将其从任何地方发送回来,递归仍然可以通过调用堆栈返回,返回它必须返回的任何值,包括undefined,反向他们被称为的顺序。 So it's actually giving your console.log call four return values: 15, undefined, undefined, undefined. 所以它实际上给你的console.log调用了四个返回值:15,undefined,undefined,undefined。

Because it synchronous, console.log can't execute until the method it called is done. 因为它是同步的,所以console.log在它调用的方法完成之前无法执行。 What it outputs is the last value it gets, or undefined. 它输出的是它获得的最后一个值,或者是未定义的。 If you stick in a return after the method call in your else block, you'll see you get 5, or the value of acc after the first iteration of the function. 如果你在else块中的方法调用后坚持返回,你会看到你得到5,或者在函数第一次迭代后得到acc的值。

var multiplyT = function(a, b, acc) {

  if (b == 0) {
    console.log("BASE CASE: ", acc);

    return acc;
  } else {
    b--;
    acc = acc + a;
    console.log("NOT THE BASE CASE: ", a,b,acc);
    multiplyT(a, b, acc);
    return acc;
  }
}
console.log(multiplyT(5,3,0));

You need to return from the else block as well. 您还需要从else块返回。

In your case even though the value of acc is updated that value is mot returned when b != 0 在你的情况下,即使acc的值被更新,当b != 0时,返回值为mot

 var multiplyT = function(a, b, acc) { if (b == 0) { console.log("BASE CASE: ", acc); return acc; } else { b--; acc = acc + a; console.log("NOT THE BASE CASE: ", a, b, acc); return multiplyT(a, b, acc); //return here } } console.log(multiplyT(5, 3, 0)); 

You're calling recursively the multiplyT function, but you're not controlling the return. 你正在递归地调用multiplyT函数,但是你没有控制返回。 Then, multiplyT(5,3,0) does not return a value in the first call and the function then returns undefined. 然后,multiplyT(5,3,0)在第一次调用中不返回值,然后函数返回undefined。

It's true that the stack is executing, but the first call is the more important: it needs to get the value from the recursive inner function that returned the final value. 堆栈正在执行,但第一次调用更重要:它需要从返回最终值的递归内部函数中获取值。

Fix the code in the else branch so you can return the recursive call: 修复else分支中的代码,以便返回递归调用:

var multiplyT = function(a, b, acc) {

    if (b == 0) {
        console.log("BASE CASE: ", acc);

        return acc;
    } else {
        b--;
        acc = acc + a;
        console.log("NOT THE BASE CASE: ", a,b,acc);
        return multiplyT(a, b, acc);
    }
}

I've come up with a good solution to this while working on one of my projects in which I traverse a complex JSON object to return some data on searching for the id. 我在处理我的一个项目时想出了一个很好的解决方案,我在其中遍历一个复杂的JSON对象,返回一些搜索id的数据。

var multiplyT = function(a, b, acc) {
   if(b == 0) {
       console.log("BASE CASE : ", acc);
       return acc;
   } else {
       var ret;
       b--; acc += a;
       console.log("NOT THE BASE CASE: ",a,b,acc);
       while( ret = multiplyT(a,b,acc) ) {
           return ret;
       }
   }
}

Here, we run a while loop to see if the recursed function call returns a true value or undefined. 在这里,我们运行一个while循环来查看递归函数调用是返回true值还是undefined。 If returns something other than undefined , it will return the data. 如果返回undefined其他内容,它将返回数据。

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

相关问题 为什么此代码中的console.log返回undefined? - Why console.log in this code return undefined? 为什么返回 console.log() 会返回 undefined? - Why does returning a console.log() return undefined? for循环,console.log,返回,未定义和作用域 - for loops, console.log, return, undefined and scope 为什么console.log(true && undefined)返回undefined而if(true && undefined)返回false? - Why console.log(true&&undefined) return undefined while if(true&&undefined) return false? JavaScript-Console.log返回一些内容,但返回时显示未定义 - JavaScript - Console.log return something but with return it shows undefined 为什么event [1]返回未定义,但console.log(event)似乎返回数组? - Why does event[1] return undefined but console.log(event) seems to return an array? 为什么map函数返回undefined但console.log注销? - Why does map function return undefined but console.log logs out? Console.log(array) 打印结果,但 return(array) 未定义 - Console.log(array) prints a result, but return(array) is undefined console.log返回值,return语句是否未定义? - console.log returns value, the return statement is undefined? 为什么节点中的 console.log(this) 返回一个空对象? - Why does console.log(this) in node return an empty object?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM