简体   繁体   English

检查Typescript / Javascript中的布尔值,分别与“ true”,“ false”和“ undefined”有关

[英]Checking for Boolean Values in Typescript/Javascript Pertaining to “true”, “false”, and “undefined”

What's an explicit way that I can be sure that the only results for a boolean check will be "true" or "false"? 我可以确保布尔检查的唯一结果是“ true”或“ false”的显式方法是什么? In other words, I want to exclude "undefined" as a possibility as much as is possible. 换句话说,我想尽可能排除“ undefined”。 Two options would be: 两种选择是:

FUNCTION ONE: 功能一:

private canMove = (currentOptionSelected): boolean => {
    if (this.client.services) {
        for (const service of this.client.services) {
            if (service === currentOptionSelected) {
                if (service.currentStage === 'enrolling') {
                    return true;
                }
            }
        }
    } 
}

FUNCTION TWO: 功能二:

private canMove = (currentOptionSelected): boolean => {
    if (this.client.services) {
        for (const service of this.client.services) {
            if (service === currentOptionSelected) {
                return service.currentStage === 'enrolling';
            }
        }
    }
}

EDIT: Upon a commenter's response, a more robust alternative is to explicitly return 'false', like this: 编辑:在评论者的回应下,一个更可靠的选择是显式返回'false',如下所示:

private canMove = (currentOptionSelected): boolean => {
    if (this.client.services) {
        for (const service of this.client.services) {
            if (service === currentOptionSelected) {
                //You should also rethink this return statement
                return service.currentStage === 'enrolling';
            }
        }
    }
    return false;
}

I would follow that up by asking if adding an extra "return 'false'" for a case where "client.services" exists, but "currentStage !== 'enrolling" would be even better? 我将通过询问是否存在“ client.services”的情况添加一个额外的“ return'false'”来跟进,但是“ currentStage!=='enrolling”会更好吗? Or would that second 'else' clause be redundant in this case? 还是在这种情况下第二个“ else”子句是多余的?

Secondly, he writes I should rethink the return statement in terms of where it is in the function. 其次,他写道,我应该根据函数中的返回位置重新考虑return语句。 What would the alternative be? 替代方案是什么? In short, I'm trying to find the most robust yet terse way to write this function. 简而言之,我正在尝试找到最健壮但简洁的方法来编写此函数。

These are not equivalent. 这些不相等。 The second version returns false in some of the cases where the first one returns undefined . 在第一个版本返回undefined某些情况下,第二个版本返回false A caller that checks the value to be explicitly === false will observe different behavior. 调用者检查该值是否明确=== false将观察到不同的行为。

Both can return undefined which is probably not great. 两者都可以返回undefined ,这可能不是很大。 It'd be best to always return with an actual value of true or false 最好总是返回truefalse的实际值

Short answer is No , they aren't equivalent: 简短的回答是No ,它们不是等效的:

  • The first one won't return false in any case. 在任何情况下,第一个都不会返回false
  • While the second can return false if service.currentStage !== 'enrolling' . 如果service.currentStage !== 'enrolling'则第二个可以返回false

But as stated by Ryan both can return undefined which you should avoid, you need to explicitly return false whenever a condition isn't met. 但是正如Ryan所述,两者都可以返回undefined ,您应该避免这种情况,只要不满足条件,就需要显式返回false

This is how should be your code: 这应该是您的代码:

private canMove = (currentOptionSelected): boolean => {
    if (this.client.services) {
        for (const service of this.client.services) {
            if (service === currentOptionSelected) {
                //You should also rethink this return statement
                return service.currentStage === 'enrolling';
            }
        }
    }
    return false;
}

Note: 注意:

  • The return false; return false; here will make sure you return false when this.client.services is undefined . this.client.services undefined时,这里将确保您返回false
  • Using a return statement in a for loop this way is a very very bad idea, in fact you will only make one iteration. 以这种方式在for循环中使用return语句是一个非常糟糕的主意,实际上您只会进行一次迭代。

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

相关问题 如果数组中的所有值都为真(字符串)并且其中一个值为假(字符串),则如何返回 boolean true 停止使用 Javascript 检查 - How to return a boolean true if all the values in an array are true (strings) and if one of the value is false(string) stop checking using Javascript 在布尔TypeScript / JavaScript方法中返回true / false的正确方法? - Right way to return true/false in boolean TypeScript/JavaScript method? 布尔值在Flash中为true,但在Javascript中为false - Boolean is true in Flash, but false in Javascript javascript:检查 boolean 值 - javascript : checking boolean values javascript if语句检查是/否 - javascript if statement checking for true/false 如何仅检查 boolean 的真/假值或 typescript 中未定义的联合类型? - How to only check true/false value for boolean or undefined union type in typescript? JavaScript布尔值true变为false,false则不变为true - Javascript boolean true turns false, false does not turn true Javascript-将1/0和'true'/'false'和true / false转换为布尔值的好方法 - Javascript - Nice way to convert 1/0 & 'true'/'false' & true/false to boolean 如何在javascript中将布尔值true或false转换为1或0 - How to convert boolean true or false into 1 Or 0 in javascript Javascript 检查字符串是否为真或假并转换为 Boolean - Javascript check if string if true or false and convert to Boolean
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM