首页 > 解决方案 > 多个函数后返回真/假

问题描述

function topFunction() {
  if (checkUserRole()) {
    //trying to figure out if I will hit this line
  }
}

checkUserRole() {
  anotherFunction()
}

anotherFunction() {
  return true;
}

所以我要问的是,checkUserRole()在这种情况下,原件会被认为是真实的吗?或者我是否需要以某种方式将trueup from传递给anotherFunction()to checkUserRole()

标签: javascript

解决方案


不,您需要明确返回它:

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


推荐阅读