为什么此回调返回未定义? [英] Why does this callback return undefined?
本文介绍了为什么此回调返回未定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
function firstFunction(num, callback) {
callback(num);
};
function secondFunction(num) {
return num + 99;
};
console.log(firstFunction(56, secondFunction));
undefined
如果我从 secondFunction
内调用 console.log
,它将返回该值。
If I call console.log
from within secondFunction
, it returns the value.
为什么不呢?如果我无法从中获取价值以备后用,设置回调有什么意义?我缺少了一些东西。
Why not? What's the point of setting up callbacks if I can't get the value out of them to use later? I'm missing something.
推荐答案
在您的函数 firstFunction
中,您做:
callback(num);
哪个计算得出
56 + 99;
那时是
155;
但是您永远不会返回该值!没有返回值,函数将简单地得出 undefined
。
But you never return the value! Without a return value, a function will simply evaluate to undefined
.
尝试这样做:
function firstFunction(num, callback) {
return callback(num);
};
这篇关于为什么此回调返回未定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文