javascript - 多个函数后返回真/假
问题描述
function topFunction() {
if (checkUserRole()) {
//trying to figure out if I will hit this line
}
}
checkUserRole() {
anotherFunction()
}
anotherFunction() {
return true;
}
所以我要问的是,checkUserRole()
在这种情况下,原件会被认为是真实的吗?或者我是否需要以某种方式将true
up from传递给anotherFunction()
to checkUserRole()
?
解决方案
不,您需要明确返回它:
function topFunction() {
if (checkUserRole()) {
//trying to figure out if I will hit this line
}
}
checkUserRole() {
return anotherFunction();
}
anotherFunction() {
return true;
}
checkUserRole
如果函数中没有返回,则返回true
的内容anotherFunction
会丢失。您最初编写它的方式从 不返回任何内容checkUserRole
,这意味着topFunction
无论在anotherFunction
或中发生什么,它都将无法通过 if 语句中的“真实”测试checkUserRole
。
推荐阅读
- regex - 正则表达式 - 如果长度超过最大允许长度,则从字符串中检索最后 n 个字符
- python - 已解决:为什么导入 Python 库会引发语法错误?
- node.js - 未捕获的类型错误:_firebase_app__WEBPACK_IMPORTED_MODULE_0___default.a.registerVersion 不是 IONIC v4 中的函数
- html - 不知道如何在 ngx-select-dropdown Angular 8 中设置选项
- python - PyCharm 创建可执行文件,ImportError: Failed to import any qt binding for QT interface
- sql - Coldfusion cfloop Sql 查询极慢
- java - 如何在java中的服务(DAO)类中获取客户端IP地址
- android - 在 Kotlin 中使用协程打开 WebView
- android - 颤振:未处理的异常:ServicesBinding.defaultBinaryMessenger
- bash - 为什么 sed 命令可以交互工作,但不能在我的脚本中工作?[更新]