javascript - 从另一个函数中的函数返回
问题描述
我有这种情况:
function f1(a) {
a = f2(a);
// do some other stuff
console.log("I don't want the function f1 to log this.")
}
function f2(a) {
if (a == null) {
console.log("enters here")
return;
}
return a;
}
f1(null)
如果a
为空,我不想f1
继续使用console.log()
. 我可以改变什么来获得这种行为?(我知道我可以用一些来做到这一点,booleans
但我想知道是否有另一种方法来解决这个问题)
解决方案
如果是,您可以存储 的最后一个值a
并退出。temp
null
function f1(a) {
let temp = a;
a = f2(a);
// do some other stuff
if (temp === null) return;
console.log("I don't want the function f1 to log this.");
}
function f2(a) {
if (a == null) {
console.log("enters here");
return;
}
return a;
}
f1(null);
推荐阅读
- android - fetchSignInMethodsForEmail 在 TOAST 上给出一个空的电子邮件字段
- algorithm - 最宽路径问题有哪些应用?
- sql - Oracle中的日期比较出现错误的情况
- java - 如果某个应用程序处于焦点位置,则检查 Java
- python-datetime - 处理偏移天真的对象
- javascript - 谷歌云功能 - 承诺导致执行错误
- c# - 在 Xamarin.Forms 中使用通用主机生成器时谁调用了 IHost.Run?
- android - 资源错误请告诉我如何解决
- html - RegEx - 排除字符和最小字母数
- verilog - 在verilog中创建一个可变宽度的常量向量