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 中的三大對象 (本地對象、內置對象、 宿主對象)
本地對象
- Object 對象屬性和方法
- String 對象屬性和方法
- Array 對象屬性和方法
- Date 對象屬性和方法
- Number 對象屬性和方法
- RegExp 對象屬性和方法
- Function 對象屬性和方法
- Boolean 對象屬性和方法
- Error 對象屬性和方法
內置對象
宿主對象