【问题标题】:Console.logging a function that sums three parameters and then console logs them outConsole.logging 一个函数,将三个参数相加,然后控制台将它们注销
【发布时间】:2023-04-11 11:55:02
【问题描述】:

代码:x = (x,y,z) => console.log(x+y+z);console.log('x is: '+ x(2,3,5)); 输出:'10' x is: undefined. 我知道当它看到函数调用时,会上升并使用给定的参数执行它,结果是“10”。然后它再次打印console.log的字符串,但是当涉及到函数调用时它返回未定义?它不应该只是在这两者之间来回循环吗?

【问题讨论】:

  • 您的函数x 不返回任何内容(基本上是无效的)。当您调用第二个console.log 时,它期望x 返回将添加到字符串中的内容。你需要返回x+y+z,而不是记录它

标签: javascript node.js console.log


【解决方案1】:

选择一个console.log 在正文中或调用函数时预定义。 x = (x,y,z) => { return(x+y+z) } console.log('x is: '+ x(2,3,5));

【讨论】:

    【解决方案2】:

    考虑一下您的代码,您只是在打印,因为函数 x 没有返回任何内容,因此预计不会打印任何其他内容

    x = (x,y,z) => console.log(x+y+z);
    console.log('x is: '+ x(2,3,5));
    

    正确的方法

    x = (x,y,z) => x+y+z; 
    console.log('x is: '+ x(2,3,5));
    

    所以不要返回任何内容,而是返回结果

    【讨论】:

    • 我知道什么是正确的方法,我只是在尝试看看会发生什么。我没有完全理解。
    • x = (x,y,z) => console.log(x+y+z); 函数x 什么也不返回,因为我说它只是打印结果,所以当你打印函数x 返回的内容时,你会得到undefined,因为函数什么都不返回但是当你像x = (x,y,z) => x+y+z; 那样做时,这意味着函数应该返回结果,这就是它起作用的原因
    猜你喜欢
    • 1970-01-01
    • 2012-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-22
    • 2010-10-01
    相关资源
    最近更新 更多