Boolean 对象属性和方法

Boolean 对象

 

在 JavaScript 中, Boolean 是一种基本的数据类型。用于将非布尔值转换为布尔值(true 或者 false)。当作为一个构造函数(带有运算符 new)调用时,Boolean() 将把它的参数转换成一个布尔值,并且返回一个包含该值的 Boolean 对象。如果作为一个函数(不带有运算符 new)调用时,Boolean() 只将把它的参数转换成一个原始的布尔值,并且返回这个值。JavaScript 中,通过逻辑与、或、非、取反进行判断一个值是否符合预期是很常见的用法。
创建一个 Boolean 对象的方法:
  var bool = Boolean(x)
  var bool = new Boolean(x)
  var bool = !x
  var bool = x === y
  var bool = x && y
  var bool = x || y
  var bool = x !== y
  var bool = false

 

Boolean 对象与其实例对象的属性和方法:

// Object.getOwnPropertyDescriptors(Boolean):
length                      : {value: 1, writable: false, enumerable: false, configurable: true}
name                        : {value: "Boolean", writable: false, enumerable: false, configurable: true}
prototype                   : {value: Boolean, writable: false, enumerable: false, configurable: false}

// Object.getOwnPropertyDescriptors(Boolean.prototype):
constructor                 : {writable: true, enumerable: false, configurable: true, value: ƒ}
toString                    : {writable: true, enumerable: false, configurable: true, value: ƒ}
valueOf                     : {writable: true, enumerable: false, configurable: true, value: ƒ}

 

 

Boolean.prototype 常用方法与描述

 

方法描述
toString() 按照布尔结果返回“true”或 “fales”。
valueOf() 返回布尔对象的原始值。

常用方法:

1、toString()

功能:根据布尔值返回字符串 "true" 或 "false"。在 Boolean 对象被用于字符串环境中时,此方法会被自动调用。

2、valueOf()

功能:返回 Boolean 对象的原始值。

 

转换成Boolean的方法

1.Boolean()

功能:把传入值转换为 true 或 false

语法:Boolean(val)

参数:val,需要转换成Boolean的值

返回值:true 或 false

示例:

console.log('bStr1',Boolean('gwg'))// true
console.log('bStr2',Boolean(' '))// true
console.log('bStr3',Boolean(''))// false
console.log('bArr1',Boolean([1,2,3]))// true
console.log('bArr2',Boolean([]))// true
console.log('bNum1',Boolean(123))// true
console.log('bNum2',Boolean(0))// false
console.log('bNum3',Boolean(-0))// fasle
console.log('bObj1',Boolean({a:1}))// true
console.log('bObj2',Boolean({}))// true
console.log('bFun1',Boolean(function () {return null}))// true
console.log('bFun2',Boolean(function () {return false}))// true
console.log('bTrue',Boolean(true))// true
console.log('bFalse',Boolean(false))// false
console.log('bNan',Boolean(NaN))// false
console.log('bNull',Boolean(null))// false
console.log('bUndf',Boolean(undefined))// false
console.log('bNot',Boolean())// false

小结:使用 Boolean() 时除了 0、''、undefined、null、NaN、false或不传任何值会转为false外,其他值都转为true

不过要注意以下情况:

// 注意:Boolean(new Boolean()) 会转为 true
console.log('bNewBool1',Boolean(new Boolean(false)))// true
console.log('bNewBool2',Boolean(new Boolean(null)))// true
console.log('bNewBool3',Boolean(new Boolean(undefined)))// true
console.log('bNewBool4',Boolean(new Boolean(0)))// true
console.log('bNewBool5',Boolean(new Boolean('')))// true
console.log('bNewBool6',Boolean(new Boolean(NaN)))// true
console.log('bNewBool7',Boolean(new Boolean()))// true

var newBool = new Boolean(false)
console.log('newBool: ' + newBool)// newBool: false
console.log('Boolean newBool: ' + Boolean(newBool))// Boolean newBool: true

// 建议使用字面量或!!,尽量不要使用 new Boolean()
var theVal
var flag1 = Boolean(theVal)
var flag2 = !!theVal
var flag3 = new Boolean(theVal)
console.log('建议使用一,Boolean(theVal):'+flag1)// 建议使用一,Boolean(theVal):false
console.log('建议使用二,!!theVal:'+flag2)// 建议使用二,!!theVal:false
console.log('不建议使用,new Boolean(theVal):'+flag3)// 不建议使用,new Boolean(theVal):false

 

 

JavaScript 中的三大对象 (本地对象、内置对象、 宿主对象)

本地对象

 

内置对象

 

宿主对象

 

posted @ 2020-06-02 15:53  elfpower  阅读(1409)  评论(0编辑  收藏  举报