[英]What's the difference between a boolean as primitive and a boolean as property of an object?
[英]What's the point of the Boolean object?
我正在閱讀關於JavaScript的Mozilla手冊,我在閱讀布爾對象時談到了這一點。 我看不到他們的一個用途。 他們有什么意義? 你為什么不使用只是true
和false
?
順便說一句,我根本不懂Java,我不怕學習新東西,因此我為什么要學習JavaScript。 我是一名PHP程序員,后端人員,我想學習如何做一些前端工作,所以我正在閱讀Mozilla JavaScript指南 。
因為(有點可悲)這是如何定義語言的 - 我懷疑它最初是為了性能/優化; 注意分配給下面的string
屬性的情況。 (Java的工作方式類似,盡管Scala和Python在很大程度上拒絕了這種區別)。
請注意, Boolean
不是唯一的“包裝器類型”。 例如,還有String
和Number
。
因此,仍存在許多怪癖(下面的內容可能同樣適用於布爾值):
typeof("foo") // string
typeof(new String("foo")) // object
"foo" instanceof String // false
new String("foo") instanceof String // true
// result is undefined: a string is a primitive and silently "ate" the assignment
// this also makes it a much cheaper value as it's not a "real" object
x = "f"; x.bar = 42; x.bar
// result is 42: a String is a "real" object with real properties!
// however, this also means that it may have a good bit more overhead
x = new String("f"); x.bar = 42; x.bar
我知道這並沒有“回答”這個問題,而是在火上扔了一些木頭;-)
從上面來看唯一真正的“問題”是,也許new Boolean(false)
是一個真值y值。
快樂的編碼。
JavaScript語言設計有很多塵埃落定的角落,布爾就是其中之一; 它沒有在實踐中使用。
這個:
var a = [];
alert(a instanceof Array);
會告訴你“真實”。 但是這個:
var b = true;
alert(b instanceof Boolean);
由於某種原因將顯示“虛假”。
簡而言之:忘了它。
創建一個新的布爾對象“基本上”運行語句中的一些代碼,然后從那里返回真正的布爾值。
來自相同的文檔:
1 var b = new Boolean(false);
2 if (b) // this condition evaluates to true
https://developer.mozilla.org/en/JavaScript/Guide/Statements#if...else_Statement
也許是因為JavaScript對象可以以原語不可擴展的方式進行擴展? (我只是在這里猜測,我從來不需要布爾值。
Boolean.prototype.bang = function() {
return !this.valueOf();
}
true.bang(); // false
JavaScript中的所有內容都是一個對象。 但與此同時我們也有原始人。 這真的令人困惑,只是不要過度思考它。
從文檔:
不要將原始布爾值true和false與Boolean對象的true和false值混淆。 任何值未定義的對象,null,0,NaN或空字符串(包括值為false的Boolean對象)在傳遞給條件語句時計算結果為true。
想象一下以下場景:
if(SomeBoolean){...}
在SomeBoolean
是布爾對象的情況下會出現這種情況。
反過來:
if(false){...}
永遠都是假的
補遺澄清。
var someString = new Boolean("MyNonEmptyString")
if(someString) //true
var otherString = new Boolean("")
if(otherString) //false
你可以使用return Boolean(something)
從任何值強制轉換為true或false,但是寫入return !!something
更短的return !!something
,這也會強制成為真或假。
我不得不支持這里的大多數人對布爾對象不太需要,但我想指出一些事情。
顯式比較仍將像布爾值一樣進行評估:
var someBool = new Boolean(false);
if (someBool == false)
alert('Got here'); //Alerts 'Got here'
正因為如此,你可以排序的將其擴展到一個子類,並仍然能夠具有上述的比較工作:
var classExtension = {
toYN: function() {
return this == false ? 'N' : 'Y';
}
};
function getNewClass(val) {
var newBool = new Boolean(val);
jQuery.extend(newBool, classExtension);
return newBool;
}
var newTest = getNewClass(false);
if (newTest)
alert('It\'s alive');
if (newTest == false)
alert('And still a bool');
alert(newTest.toYN());
這將警告'它還活着','仍然是一個布爾'和'N'。 http://jsfiddle.net/fkJuk/
但是,你真的需要這個嗎? 即使你這樣做,也可能更好的方法是讓你自己的單獨類具有一個被檢查的布爾屬性。 最后,這可能是為了保持一致性; 每個原語都有JavaScript中的直接構造函數和原型訪問。
回到規范(ECMA-262.pdf第151頁),請注意當布爾值作為函數而不是構造函數調用時,它會進行類型轉換。 從而:
var t = 5
, f = 0
console.log(Boolean(t)) //Prints true
console.log(Boolean(f)) //Prints false
當然,作為一個JavaScript對象,您可以使用'new'運算符將其用作原型,正如其他人所指出的那樣,但我認為沒有任何理由這樣做。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.