[英]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
最好总是返回true
或false
的实际值
Short answer is No , they aren't equivalent: 简短的回答是No ,它们不是等效的:
false
in any case. 在任何情况下,第一个都不会返回false
。 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: 注意:
return false;
return false;
here will make sure you return false
when this.client.services
is undefined
. 当this.client.services
undefined
时,这里将确保您返回false
。 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.