aboutsummaryrefslogtreecommitdiff
path: root/files/zh-tw/web/javascript/reference/global_objects/boolean/index.html
blob: d35d07470b0c8caaa5038214acf703faf23a697f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
---
title: Boolean
slug: Web/JavaScript/Reference/Global_Objects/Boolean
translation_of: Web/JavaScript/Reference/Global_Objects/Boolean
---
<div>{{JSRef}}</div>

<p><strong><code>Boolean</code></strong> 是布林值的包覆器。</p>

<h2 id="語法">語法</h2>

<pre class="syntaxbox">new Boolean([<var>value</var>])</pre>

<h3 id="參數">參數</h3>

<dl>
 <dt><code>value</code> {{optional_inline}}</dt>
 <dd>這個<code>Boolean</code>物件的初始值。</dd>
</dl>

<h2 id="說明">說明</h2>

<p>傳入的第一個參數值,如果需要的話,會被轉換成布林值。如果沒傳值,或者是<code>0</code><code>-0</code>{{jsxref("null")}}<code>false</code>{{jsxref("NaN")}}{{jsxref("undefined")}}、空字串(<code>""</code>)的話,這個物件的值會被初始化成<code>false</code>。大多數情況下,DOM 物件 {{domxref("document.all")}} 被傳入後,也會將其初始化為<code>false</code>。至於其他的值,包含所有物件或<code>"false"</code>字串,都會使其初始化為<code>true</code></p>

<p>不要將原始型別的布林值和這個布林物件搞混,它們並不相同。</p>

<p>在判斷式中,任何物件只要不是 {{jsxref("undefined")}} 或 {{jsxref("null")}} ,儘管是值為<code>false</code> 的 <code>Boolean</code> 物件,都會被轉換成<code>true</code>。舉例來說,下列的 {{jsxref("Statements/if...else", "if")}} 判斷式中的布林值即為<code>true</code></p>

<pre class="brush: js">var x = new Boolean(false);
if (x) {
  // this code is executed
}
</pre>

<p>以上這個行為和<code>Boolean</code>原始型別沒有關連,反倒是下面的 {{jsxref("Statements/if...else", "if")}} 判斷式會正確地將其視為<code>false</code></p>

<pre class="brush: js">var x = false;
if (x) {
  // this code is not executed
}
</pre>

<p>不要用<code>Boolean</code>物件將非布林值轉換成布林值。反而要將<code>Boolean</code>視為函式去轉換非布林值:</p>

<pre class="brush: js">var x = Boolean(expression);     // 較好
var x = new Boolean(expression); // 不要用
</pre>

<p>如果你要指定任何物件,包括值為<code>false</code><code>Boolean</code>物件,作為<code>Boolean</code>物件的初始值,則該<code>Boolean</code>物件的值依舊為<code>true</code></p>

<pre class="brush: js">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
</pre>

<p>不要使用<code>Boolean</code>物件代替<code>Boolean</code>的原始型別!</p>

<h2 id="屬性">屬性</h2>

<dl>
 <dt><code>Boolean.length</code></dt>
 <dd>長度永遠為1。</dd>
 <dt>{{jsxref("Boolean.prototype")}}</dt>
 <dd>原型為<code>Boolean</code>的建構式。</dd>
</dl>

<h2 id="方法">方法</h2>

<p>全域的<code>Boolean</code>物件自身沒有任何方法,它只有從原型鏈繼承而來的方法。</p>

<h2 id="Boolean_實體"><code>Boolean</code> 實體</h2>

<p>所有 <code>Boolean</code> 實體會繼承 {{jsxref("Boolean.prototype")}} 。和所有建構式一樣,原型物件會指派給實體那些繼承的屬性和方法。</p>

<h3 id="屬性_2">屬性</h3>

<div>{{page('/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean/prototype', 'Properties')}}</div>

<h3 id="方法_2">方法</h3>

<div>{{page('/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean/prototype', 'Methods')}}</div>

<h2 id="範例">範例</h2>

<h3 id="用_false_作為初始值建立_Boolean_物件"><code>false</code> 作為初始值建立 <code>Boolean</code> 物件</h3>

<pre class="brush: js">var bNoParam = new Boolean();
var bZero = new Boolean(0);
var bNull = new Boolean(null);
var bEmptyString = new Boolean('');
var bfalse = new Boolean(false);
</pre>

<h3 id="用_true_作為初始值建立_Boolean_物件"><code>true</code> 作為初始值建立 <code>Boolean</code> 物件</h3>

<pre class="brush: js">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({});
</pre>

<h2 id="規範">規範</h2>

{{Specifications}}

<h2 id="瀏覽器相容性">瀏覽器相容性</h2>

<div>{{CompatibilityTable}}</div>

{{Compat}}

<h2 id="參見">參見</h2>

<ul>
 <li>{{jsxref("Boolean.prototype")}}</li>
 <li>{{Glossary("Boolean")}}</li>
 <li><a href="http://en.wikipedia.org/wiki/Boolean_data_type">Boolean data type (Wikipedia)</a></li>
</ul>