Boolean
是布林值的包覆器。
語法
new Boolean([value])
參數
value
選擇性- 這個
Boolean
物件的初始值。
說明
傳入的第一個參數值,如果需要的話,會被轉換成布林值。如果沒傳值,或者是0
、-0
、null
、false
、NaN
、undefined
、空字串(""
)的話,這個物件的值會被初始化成false
。大多數情況下,DOM 物件 document.all
被傳入後,也會將其初始化為false
。至於其他的值,包含所有物件或"false"
字串,都會使其初始化為true
。
不要將原始型別的布林值和這個布林物件搞混,它們並不相同。
在判斷式中,任何物件只要不是 undefined
或 null
,儘管是值為false
的 Boolean
物件,都會被轉換成true
。舉例來說,下列的 if
判斷式中的布林值即為true
:
var x = new Boolean(false);
if (x) {
// this code is executed
}
以上這個行為和Boolean
原始型別沒有關連,反倒是下面的 if
判斷式會正確地將其視為false
:
var x = false;
if (x) {
// this code is not executed
}
不要用Boolean
物件將非布林值轉換成布林值。反而要將Boolean
視為函式去轉換非布林值:
var x = Boolean(expression); // 較好
var x = new Boolean(expression); // 不要用
如果你要指定任何物件,包括值為false
的Boolean
物件,作為Boolean
物件的初始值,則該Boolean
物件的值依舊為true
。
var myFalse = new Boolean(false); // 初始值給false,實際上為true
var g = new Boolean(myFalse); // 想當然耳,true
var myString = new String('Hello'); // 字串物件,'Hello'
var s = new Boolean(myString); // 依舊為true
不要使用Boolean
物件代替Boolean
的原始型別!
屬性
Boolean.length
- 長度永遠為1。
Boolean.prototype
- 原型為
Boolean
的建構式。
方法
全域的Boolean
物件自身沒有任何方法,它只有從原型鏈繼承而來的方法。
Boolean 實體
所有 Boolean
實體會繼承 Boolean.prototype
。和所有建構式一樣,原型物件會指派給實體那些繼承的屬性和方法。
屬性
Boolean.prototype.constructor
- Returns the function that created an instance's prototype. This is the
Boolean
function by default.
方法
Boolean.prototype.toSource()
- Returns a string containing the source of the
Boolean
object; you can use this string to create an equivalent object. Overrides theObject.prototype.toSource()
method. Boolean.prototype.toString()
- Returns a string of either
"true"
or"false"
depending upon the value of the object. Overrides theObject.prototype.toString()
method. Boolean.prototype.valueOf()
- Returns the primitive value of the
Boolean
object. Overrides theObject.prototype.valueOf()
method.
範例
用 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 | Initial definition. Implemented in JavaScript 1.0. |
ECMAScript 5.1 (ECMA-262) The definition of 'Boolean' in that specification. |
Standard | |
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Boolean' in that specification. |
Standard | |
ECMAScript Latest Draft (ECMA-262) The definition of 'Boolean' in that specification. |
Draft |
瀏覽器相容性
We're converting our compatibility data into a machine-readable JSON format.
This compatibility table still uses the old format,
because we haven't yet converted the data it contains.
Find out how you can help!
Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Basic support | (Yes) | (Yes) | (Yes) | 6.0 | (Yes) | (Yes) |
Feature | Android | Chrome for Android | Edge | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|---|
Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |