Boolean
对象是一个布尔值的对象包装器。
语法
new Boolean([value])
参数
-
value
-
可选,用来初始化
Boolean
对象的值。
描述
如果第一个参数不是布尔值,则会将其转换为布尔值。如果省略该参数,或者其值为 0
、-0
、document.all
,也会生成值为 false
的 Boolean
对象。任何其他的值,包括值为 "false"
的字符串和任何对象,都会创建一个值为 true
的 Boolean
对象。
注意不要将基本类型中的布尔值 true
和 false
与值为 true
和 false
的 Boolean
对象弄混了。
任何不是 if
语句中的条件为真:
var x = new Boolean(false); if (x) { // 这里的代码会被执行 }
基本类型的布尔值不受此规则影响。例如下面的 if
语句的条件为假:
var x = false; if (x) { // 这里的代码不会执行 }
不要用创建 Boolean
对象的方式将一个非布尔值转化成布尔值,直接将 Boolean
当做转换函数来使用即可,或者使用双重非(!!)运算符:
var x = Boolean(expression); // 推荐 var x = !!(expression); // 推荐 var x = new Boolean(expression); // 不太好
对于任何对象,即使是值为 false
的 Boolean
对象,当将其传给 Boolean
函数时,生成的 Boolean
对象的值都是 true
。
var myFalse = new Boolean(false); // false var g = new Boolean(myFalse); // true var myString = new String("Hello"); var s = new Boolean(myString); // true
最后,不要在应该使用基本类型布尔值的地方使用 Boolean
对象。
属性
-
Boolean.length
-
length
属性,值为 1。 -
Boolean.prototype
-
Boolean
构造函数的原型对象。
方法
Boolean
对象自身没有任何方法,不过它从自己的原型链上继承了一些方法,见下面的“Boolean
实例”小节。
Boolean
实例
所有 Boolean
实例都继承于 Boolean.prototype
。与所有的构造函数一样,Boolean
的原型对象为其实例提供继承属性和方法。
属性
-
Boolean.prototype.constructor
-
返回创建了实例原型的函数。默认为
Boolean
函数。
方法
-
Boolean.prototype.toSource()
-
返回包含
Boolean
对象源码的字符串;你可以使用这个字符串来创建一个等价的对象。覆盖了Object.prototype.toSource()
方法。 -
Boolean.prototype.toString()
-
根据对象的值来返回一个字符串:
"true"
或"false"
。覆盖了Object.prototype.toString()
方法。 -
Boolean.prototype.valueOf()
-
返回
Boolean
对象的原始值。覆盖了Object.prototype.valueOf()
方法。
示例
创建值为 false
的 Boolean
对象
var bNoParam = new Boolean(); var bZero = new Boolean(0); var bNull = new Boolean(null); var bEmptyString = new Boolean(''); var bfalse = new Boolean(false);
创建值为 true
的 Boolean
对象
var btrue = new Boolean(true); var btrueString = new Boolean('true'); var bfalseString = new Boolean('false'); var bSuLin = new Boolean('Su Lin'); var bArrayProto = new Boolean([]); var bObjProto = new Boolean({});
规范
规范 | 状态 | 说明 |
---|---|---|
ECMAScript 1st Edition (ECMA-262) | Standard | 初始定义,在 JavaScript 1.0 中实现 |
ECMAScript 5.1 (ECMA-262) Boolean |
Standard | |
ECMAScript 2015 (6th Edition, ECMA-262) Boolean |
Standard | |
ECMAScript Latest Draft (ECMA-262) Boolean |
Draft |
浏览器兼容性
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out
https://github.com/mdn/browser-compat-data and send us a pull request.
Update compatibility data on GitHub
Desktop | Mobile | Server | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Boolean |
Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 3 | Opera Full support Yes | Safari Full support Yes | WebView Android Full support Yes | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
prototype |
Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 3 | Opera Full support Yes | Safari Full support Yes | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
toSource
|
Chrome No support No | Edge No support No | Firefox Full support 1 | IE No support No | Opera No support No | Safari No support No | WebView Android No support No | Chrome Android No support No | Firefox Android Full support 4 | Opera Android No support No | Safari iOS No support No | Samsung Internet Android No support No | nodejs No support No |
toString |
Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 3 | Opera Full support Yes | Safari Full support Yes | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
valueOf |
Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 4 | Opera Full support Yes | Safari Full support Yes | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
Legend
- Full support
- Full support
- No support
- No support
- Non-standard. Expect poor cross-browser support.
- Non-standard. Expect poor cross-browser support.