If 语句检查该变量是一个布尔值结果 "boolean != boolean"
If statements to check that variable is a boolean results in "boolean != boolean"
我试图将一些错误传递到我的一些函数中,这些函数检查是否启用了特定字段。我想添加一些条件检查以确保函数的特定参数始终是布尔值(即 true 以验证该字段已启用/ false 以验证该字段已禁用)因为我注意到我的函数不会产生错误如果使用了不同的数据类型。
所以我设置了一些测试代码,它也允许用户使用某些字符串来代替真正的布尔值。看起来像这样:
(注意:我们正在使用 Squish 进行 GUI 测试并使用他们的一些内部功能)
function boolCheck(bool)
{
var truth = ["true","True","TRUE","T","t"];
var lies = ["false","False","FALSE","F","f"];
if (truth.indexOf(bool) !== -1)
{
bool = true;
}
if (lies.indexOf(bool) !== -1)
{
bool = false;
}
else if (bool !== true || bool !== false)
{
test.fatal("ERROR: " + (typeof bool).toUpperCase() + " is not a BOOLEAN data type.");
}
}
function main()
{
boolCheck(true);
}
运行 这会在本质上是控制台的内容中显示以下结果(测试。[something] 基本上就像打印到控制台)
Fatal ERROR: BOOLEAN is not a BOOLEAN data type.
这对我来说似乎很奇怪。
此外,运行主要是:
function main()
{
boolCheck("T")
}
给出这个结果:
Fatal ERROR: BOOLEAN is not a BOOLEAN data type.
显然,这一切的处理方式有问题。也许我只是以错误的方式去做这件事。任何人都可以提供一些关于为什么 "BOOLEAN" 和 "BOOLEAN" 不平等的见解吗?或者更好的方法来处理这个时期?例如,我很乐意想出一种将所有内容包装在 Try/Catch.
中的好方法
简单的逻辑错误
if (bool !== true || bool !== false)
所以让我们大声读出来。如果 bool 不等于 true 或 bool 不等于 false,则进入此块。
因此,当 bool 为 true
时,它不是假的,因此它将进入 if 块。
如果 bool 不等于 true 则需要 AND bool 不等于 false
为什么不使用 npm 模块
boolean
符合您的要求
const boolean = require('boolean');
console.log(boolean('t')) // true
console.log(boolean('True')) // true
console.log(boolean(true)) // true
console.log(boolean('TrUe')) // true
console.log(boolean('TRUE')) // true
console.log(boolean('true')) // true
console.log(boolean('f')) // false
console.log(boolean('False')) // false
console.log(boolean(false)) // false
console.log(boolean('FaLsE')) // false
console.log(boolean('FALSE')) // false
console.log(boolean('false')) // false
我试图将一些错误传递到我的一些函数中,这些函数检查是否启用了特定字段。我想添加一些条件检查以确保函数的特定参数始终是布尔值(即 true 以验证该字段已启用/ false 以验证该字段已禁用)因为我注意到我的函数不会产生错误如果使用了不同的数据类型。
所以我设置了一些测试代码,它也允许用户使用某些字符串来代替真正的布尔值。看起来像这样: (注意:我们正在使用 Squish 进行 GUI 测试并使用他们的一些内部功能)
function boolCheck(bool)
{
var truth = ["true","True","TRUE","T","t"];
var lies = ["false","False","FALSE","F","f"];
if (truth.indexOf(bool) !== -1)
{
bool = true;
}
if (lies.indexOf(bool) !== -1)
{
bool = false;
}
else if (bool !== true || bool !== false)
{
test.fatal("ERROR: " + (typeof bool).toUpperCase() + " is not a BOOLEAN data type.");
}
}
function main()
{
boolCheck(true);
}
运行 这会在本质上是控制台的内容中显示以下结果(测试。[something] 基本上就像打印到控制台)
Fatal ERROR: BOOLEAN is not a BOOLEAN data type.
这对我来说似乎很奇怪。
此外,运行主要是:
function main()
{
boolCheck("T")
}
给出这个结果:
Fatal ERROR: BOOLEAN is not a BOOLEAN data type.
显然,这一切的处理方式有问题。也许我只是以错误的方式去做这件事。任何人都可以提供一些关于为什么 "BOOLEAN" 和 "BOOLEAN" 不平等的见解吗?或者更好的方法来处理这个时期?例如,我很乐意想出一种将所有内容包装在 Try/Catch.
中的好方法简单的逻辑错误
if (bool !== true || bool !== false)
所以让我们大声读出来。如果 bool 不等于 true 或 bool 不等于 false,则进入此块。
因此,当 bool 为 true
时,它不是假的,因此它将进入 if 块。
如果 bool 不等于 true 则需要 AND bool 不等于 false
为什么不使用 npm 模块 boolean
符合您的要求
const boolean = require('boolean');
console.log(boolean('t')) // true
console.log(boolean('True')) // true
console.log(boolean(true)) // true
console.log(boolean('TrUe')) // true
console.log(boolean('TRUE')) // true
console.log(boolean('true')) // true
console.log(boolean('f')) // false
console.log(boolean('False')) // false
console.log(boolean(false)) // false
console.log(boolean('FaLsE')) // false
console.log(boolean('FALSE')) // false
console.log(boolean('false')) // false