【发布时间】:2020-03-11 11:23:27
【问题描述】:
我无法在 NodeJS 中打印正确的结果,为什么我的代码没有以正确的方式打印字符串?我觉得 console.log 根本没有被调用。为什么我会得到:
[Function]
[Function]
[Function]
[Function]
预期结果:
Tigrou (buddy of Spider) was here!
Spider (buddy of Tigrou) was also here!
Tigrou (buddy of Spider) are in a boat...
1 (buddy of 2)3
我认为可行的代码:
function build_sentence(...args)
{
var i = 0
var s = ""
for (let arg of args)
{
if (i == 1)
{
i++
s += "(buddy of " + arg + ") "
}
else
{
s += arg + " "
i++
}
}
return s
}
function mycurry(build_sentence)
{
return function(...args)
{
if (!args)
{
return build_sentence();
}
else
{
return mycurry(build_sentence.bind(this, ...args))
}
}
}
const curried = mycurry(build_sentence);
console.log(curried("Tigrou")("Spider")(" was here!"))
console.log(curried("Spider")("Tigrou")(" was also here!"))
console.log(curried("Tigrou", "Spider", " are in a boat... "))
console.log(curried(1)(2, 3))
【问题讨论】:
-
您正在传递一个函数(由您提供的输出理解),因此您必须调用该函数才能真正从中获取您想要的值。
-
是的,我在最后 5 行调用了函数
标签: javascript node.js function printing arguments