简体   繁体   English

在 JavaScript 函数中返回布尔值

[英]Returning a boolean value in a JavaScript function

I am doing a client side form validation to check if passwords match.我正在进行客户端表单验证以检查密码是否匹配。 But the validation function always returns undefined .但是验证函数总是返回undefined

function validatePassword(errorMessage)
{
   var password = document.getElementById("password");
   var confirm_password = document.getElementById("password_confirm");

   if(password.value)
   {
      // Check if confirm_password matches
      if(password.value != confirm_password.value)
      {
         return false;
      }
   }
   else
   {
      // If password is empty but confirm password is not
      if(confirm_password.value)
      {
         return false;
      }
   }
   return true;
}

Please note that the validatePassword is called from a member function of the Form object.请注意, validatePassword是从 Form 对象的成员函数调用的。

function Form(validation_fn)
{
   // Do other stuff
   this.submit_btn = document.getElementById("submit");
   this.validation_fn = validation_fn;
}

Form.prototype.submit = funciton()
{
   var result;
   if(this.validation_fn)
   {
      result = this.validation_fn();
   }

   //result is always undefined
   if(result)
   {
       //do other stuff
   }
}

You could simplify this a lot:你可以简化很多:

  • Check whether one is not empty检查一个是否为空
  • Check whether they are equal检查它们是否相等

This will result in this, which will always return a boolean.这将导致这个,它总是返回一个布尔值。 Your function also should always return a boolean, but you can see it does a little better if you simplify your code:你的函数也应该总是返回一个布尔值,但是如果你简化你的代码,你会发现它会做得更好:

function validatePassword()
{
   var password = document.getElementById("password");
   var confirm_password = document.getElementById("password_confirm");

   return password.value !== "" && password.value === confirm_password.value;
       //       not empty       and              equal
}

You could wrap your return value in the Boolean function您可以将返回值包装在布尔函数中

Boolean([return value])

That'll ensure all falsey values are false and truthy statements are true.这将确保所有 falsey 值都是 false 并且 true 陈述是 true。

An old thread, sure, but a popular one apparently.一个旧线程,当然,但显然是一个流行的线程。 It's 2020 now and none of these answers have addressed the issue of unreadable code.现在是 2020 年,这些答案都没有解决不可读代码的问题。 @pimvdb's answer takes up less lines, but it's also pretty complicated to follow. @pimvdb 的答案占用的行数较少,但遵循起来也很复杂。 For easier debugging and better readability, I should suggest refactoring the OP's code to something like this, and adopting an early return pattern, as this is likely the main reason you were unsure of why the were getting undefined:为了更容易调试和更好的可读性,我应该建议将 OP 的代码重构为这样的东西,并采用早期返回模式,因为这可能是您不确定为什么未定义的主要原因:

function validatePassword() {
   const password = document.getElementById("password");
   const confirm_password = document.getElementById("password_confirm");

   if (password.value.length === 0) {
      return false;
   }

   if (password.value !== confirm_password.value) {
      return false;
   }
  
   return true;
}

Don't forget to use var/let while declaring any variable.See below examples for JS compiler behaviour.在声明任何变量时不要忘记使用 var/let。有关 JS 编译器行为的示例,请参见下面的示例。

function  func(){
return true;
}

isBool = func();
console.log(typeof (isBool));   // output - string


let isBool = func();
console.log(typeof (isBool));   // output - boolean

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM