判断是否为布尔值的方法

2023-12-01 0 240

判断是否为布尔值的方法

Boolean() 构造函数可用于创建布尔对象以及布尔原始值,表示 true 或 false 值。

在下面的代码中,我详细介绍了 JavaScript 中布尔值的创建。

示例:sample52.html

<!DOCTYPE html><html lang=\”en\”><body><script>

// Create a Boolean object using the new keyword and the Boolean() constructor.
var myBoolean1 = new Boolean(false); // Using new keyword.
console.log(typeof myBoolean1); // Logs \’object\’.

// Create a Boolean literal/primitive by directly using the number constructor without new.
var myBoolean2 = Boolean(0); // Without new keyword.
console.log(typeof myBoolean2); // Logs \’boolean\’.

// Create Boolean literal/primitive (constructor leveraged behind the scenes).
var myBoolean3 = false;
console.log(typeof myBoolean3); // Logs \’boolean\’.
console.log(myBoolean1, myBoolean2, myBoolean3); // Logs false false false.

</script></body></html>


Boolean() 参数

Boolean() 构造函数将一个参数转换为布尔值(即 true 或 false)。任何非 0、-0、null、false、NaN、undefined 或空字符串 (\”\”) 的有效 JavaScript 值都将转换为 true。在以下示例中,我们创建两个布尔对象值:一个 true 和一个 false。

示例:sample53.html

<!DOCTYPE html><html lang=\”en\”><body><script>

// Parameter passed to Boolean() = 0 = false, thus foo = false
var foo = new Boolean(0)
console.log(foo);

// Parameter passed to Boolean() = Math = true, thus bar = true
var bar = new Boolean(Math)
console.log(bar);

</script></body></html>

当与 new 关键字一起使用时,来自 Boolean() 构造函数的实例会生成一个实际的复杂对象。您应该避免使用 Boolean() 构造函数创建布尔值(而是使用文字/原始数字),因为存在与 typeof 运算符相关的潜在问题。 typeof 运算符将布尔对象报告为“object”,而不是您可能期望的原始标签(“boolean”)。此外,文字/原始值的写入速度更快。


Boolean() 属性和方法

Boolean() 对象具有以下属性:

属性(例如,Boolean.prototype;):

  • 原型

布尔对象实例属性和方法

布尔对象实例具有以下属性和方法(不包括继承的属性和方法):

实例属性(例如,var myBoolean = false; myBoolean.constructor;):

  • 构造函数

实例方法(例如,var myNumber = false; myBoolean.toString();):

  • toSource()
  • toString()
  • valueOf()

非原始 False 布尔对象转换为 True

从 Boolean() 构造函数创建的 false 布尔对象(而不是原始值)是一个对象,并且对象会转换为 true。因此,当通过 Boolean() 构造函数创建 false 布尔对象时,该值本身会转换为 true。在下面的示例中,我演示了 false 布尔对象如何始终是“true”。

示例:sample54.html

<!DOCTYPE html><html lang=\”en\”><body><script>

var falseValue = new Boolean(false);

console.log(falseValue); // We have a false Boolean object, but objects are truthy.

if (falseValue) { // Boolean objects, even false Boolean objects, are truthy.
console.log(\’falseValue is truthy\’);
}

</script></body></html>

如果需要将非布尔值转换为布尔值,只需使用 Boolean() 构造函数,而不使用 new 关键字,返回的值将是原始值而不是布尔对象。


某些事情是假的,其他一切都是真的

已经提到过,但值得再次提及,因为它与转换有关:如果值为 0、-0、null、false、NaN、undefined,或空字符串( \”\”),就是false。如果在布尔上下文中使用,除上述值之外的 JavaScript 中的任何值都将转换为 true(即 if (true) {};)。

示例:sample55.html

<!DOCTYPE html><html lang=\”en\”><body><script>

// All of these return a false Boolean value.
console.log(Boolean(0));
console.log(Boolean(-0));
console.log(Boolean(null));
console.log(Boolean(false));
console.log(Boolean(\’\’));
console.log(Boolean(undefined));
console.log(Boolean(null));

// All of these return a true Boolean value.
console.log(Boolean(1789));
console.log(Boolean(\’false\’)); // \’false\’ as a string is not false the Boolean value.
console.log(Boolean(Math));
console.log(Boolean(Array()));

</script></body></html>


结论

了解哪些 JavaScript 值被简化为 false 至关重要,这样您就知道所有其他值都被视为 true。

以上就是判断是否为布尔值的方法的详细内容,更多请关注悠久资源其它相关文章!

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

悠久资源 Wordpress教程 判断是否为布尔值的方法 https://www.u-9.cn/jiaocheng/wordpress-jiaocheng/16118.html

常见问题

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务