Boolean 布爾值 對象表示兩個值:"true" 或 "false"。
創(chuàng)建 Boolean 對象的語法:
var test=new Boolean(1);
console.log(test);
var test1=Boolean(1);
console.log(test1);
Paste_Image.png
返回值
當(dāng)作為一個構(gòu)造函數(shù)(帶有運(yùn)算符 new)調(diào)用時,Boolean() 將把它的參數(shù)轉(zhuǎn)換成一個布爾值,并且返回一個包含該值的 Boolean 對象。
如果作為一個函數(shù)(不帶有運(yùn)算符 new)調(diào)用時,Boolean() 只將把它的參數(shù)轉(zhuǎn)換成一個原始的布爾值,并且返回這個值。
注釋:如果省略 value 參數(shù),或者設(shè)置為 0、-0、null、""、false、undefined 或 NaN,則該對象設(shè)置為 false。否則設(shè)置為 true(即使 value 參數(shù)是字符串 "false")。
Boolean 對象屬性
屬性 | 描述 |
---|---|
constructor | 返回對創(chuàng)建此對象的 Boolean 函數(shù)的引用 |
prototype | 使您有能力向?qū)ο筇砑訉傩院头椒ā?/td> |
constructor 屬性返回對創(chuàng)建此對象的 Boolean 函數(shù)的引用。
得出創(chuàng)建此對象的構(gòu)造函數(shù)
var test=new Boolean();
console.log(test.constructor==Boolean);//true
var test1=new Array();
console.log(test1.constructor==Array);//true
var test2=new Date();
console.log(test2.constructor==Date);//true
var test3=new String();
console.log(test3.constructor==String);//true
prototype 屬性使您有能力向?qū)ο筇砑訉傩院头椒ā?/strong>
Boolean.prototype.s=1;
var test=new Boolean();
console.log(test);
Paste_Image.png
Boolean 對象方法
方法 | 描述 |
---|---|
toSource() | 返回該對象的源代碼。 |
toString() | 把邏輯值轉(zhuǎn)換為字符串,并返回結(jié)果。 |
valueOf() | 返回 Boolean 對象的原始值。 |
toSource() 方法返回表示對象源代碼的字符串。
注釋:該方法在 Internet Explorer 中無效。
var test=new Boolean();
console.log(test.toSource());//(new Boolean(false))
var test1="你好";
console.log(test1.toSource());//(new String("\u4F60\u597D"))
var test2=[1,2,3,4];
console.log(test2.toSource());//[1, 2, 3, 4]
var test3={"a":1};
console.log(test3.toSource());//({a:1})
toString() 方法可把一個邏輯值轉(zhuǎn)換為字符串,并返回結(jié)果。
//使用 toString() 來把一個布爾值轉(zhuǎn)換成字符串。
var test=new Boolean(1);
console.log(test.toString());//true
valueOf() 方法可返回 Boolean 對象的原始值。
語法
booleanObject.valueOf()
//使用 valueOf() 來取得一個 Boolean 對象的原始值。
var test=new Boolean(1);
console.log(test.valueOf());//true