mozilla

版本 259898 / JavaScript_1.5_核心参考/全局对象/Boolean对象

  • 版本网址缩略名: JavaScript_1.5_核心参考/全局对象/Boolean对象
  • 版本标题: JavaScript_1.5_核心参考/全局对象/Boolean对象
  • 版本 id: 259898
  • 创建于:
  • 创建者: Mickeyboy
  • 是否是当前版本?
  • 评论 New page: <breadcrumbs></breadcrumbs> == Summary == '''Core Object''' The <code>Boolean</code> object is an object wrapper for a boolean value. == Created by == The <code>Boolean</code> constru...

修订内容

Summary

Core Object

The Boolean object is an object wrapper for a boolean value.

Created by

The Boolean constructor:

new Boolean(value)

Parameters

value 
The initial value of the Boolean object. The value is converted to a boolean value, if necessary. If value is omitted or is 0, -0, null, false, NaN, undefined, or the empty string (""), the object has an initial value of false. All other values, including any object or the string "false", create an object with an initial value of true.

Description

Do not confuse the primitive Boolean values true and false with the true and false values of the Boolean object.

Any object whose value is not undefined or null, including a Boolean object whose value is false, evaluates to true when passed to a conditional statement. For example, the condition in the following if statement evaluates to true:

x = new Boolean(false);
if (x) //the condition is true

This behavior does not apply to Boolean primitives. For example, the condition in the following if statement evaluates to false:

x = false;
if (x) //the condition is false

Do not use a Boolean object to convert a non-boolean value to a boolean value. Instead, use Boolean as a function to perform this task:

x = Boolean(expression);     //preferred
x = new Boolean(expression); //don't use

If you specify any object, including a Boolean object whose value is false, as the initial value of a Boolean object, the new Boolean object has a value of true.

myFalse = new Boolean(false);   // initial value of false
g = new Boolean(myFalse);       //initial value of true
myString = new String("Hello"); // string object
s = new Boolean(myString);      //initial value of true

Do not use a Boolean object in place of a Boolean primitive.

Backward compatibility

JavaScript 1.2 and earlier versions

The Boolean object behaves as follows:

  • When a Boolean object is used as the condition in a conditional test, JavaScript returns the value of the Boolean object. For example, a Boolean object whose value is false is treated as the primitive value false, and a Boolean object whose value is true is treated as the primitive value true in conditional tests. If the Boolean object is a false object, the conditional statement evaluates to false.
  • You can use a Boolean object in place of a Boolean primitive.

Properties

constructor: Specifies the function that creates an object's prototype.

prototype: Defines a property that is shared by all Boolean objects.

Methods

toSource: Returns an object literal representing the specified Boolean object; you can use this value to create a new object. Overrides the Object.toSource method.

toString: Returns a string representing the specified object. Overrides the Object.toString method.

valueOf: Returns the primitive value of a Boolean object. Overrides the Object.valueOf method.

In addition, this object inherits the watch and unwatch methods from Object.

Examples

Creating Boolean objects with an initial value of false

bNoParam = new Boolean();
bZero = new Boolean(0);
bNull = new Boolean(null);
bEmptyString = new Boolean("");
bfalse = new Boolean(false);

Creating Boolean objects with an initial value of true

btrue = new Boolean(true);
btrueString = new Boolean("true");
bfalseString = new Boolean("false");
bSuLin = new Boolean("Su Lin");
{{ languages( { "fr": "fr/R\u00e9f\u00e9rence_de_JavaScript_1.5_Core/Objets_globaux/Boolean", "ja": "ja/Core_JavaScript_1.5_Reference/Global_Objects/Boolean", "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Obiekty/Boolean" } ) }}

修订版来源

<p>
</p>
<h3 id="Summary" name="Summary"> Summary </h3>
<p><b>Core Object</b>
</p><p>The <code>Boolean</code> object is an object wrapper for a boolean value.
</p>
<h3 id="Created_by" name="Created_by"> Created by </h3>
<p>The <code>Boolean</code> constructor:
</p>
<pre class="eval">new Boolean(<i>value</i>)
</pre>
<h3 id="Parameters" name="Parameters"> Parameters </h3>
<dl><dt> <code>value</code> </dt><dd> The initial value of the <code>Boolean</code> object. The value is converted to a <code>boolean</code> value, if necessary. If value is omitted or is 0, -0, null, false, <code>NaN</code>, undefined, or the empty string (""), the object has an initial value of false. All other values, including any object or the string "<code>false</code>", create an object with an initial value of true.
</dd></dl>
<h3 id="Description" name="Description"> Description </h3>
<p>Do not confuse the primitive Boolean values true and false with the true and false values of the Boolean object.
</p><p>Any object whose value is not <code>undefined</code> or <code>null</code>, including a Boolean object whose value is false, evaluates to true when passed to a conditional statement. For example, the condition in the following if statement evaluates to true:
</p>
<pre class="eval">x = new Boolean(false);
if (x) //the condition is true
</pre>
<p>This behavior does not apply to Boolean primitives. For example, the condition in the following if statement evaluates to <code>false</code>:
</p>
<pre class="eval">x = false;
if (x) //the condition is false
</pre>
<p>Do not use a <code>Boolean</code> object to convert a non-boolean value to a boolean value. Instead, use Boolean as a function to perform this task:
</p>
<pre class="eval">x = Boolean(expression);     //preferred
x = new Boolean(expression); //don't use
</pre>
<p>If you specify any object, including a Boolean object whose value is false, as the initial value of a Boolean object, the new Boolean object has a value of true.
</p>
<pre class="eval">myFalse = new Boolean(false);   // initial value of false
g = new Boolean(myFalse);       //initial value of true
myString = new String("Hello"); // string object
s = new Boolean(myString);      //initial value of true
</pre>
<p>Do not use a Boolean object in place of a Boolean primitive. </p>
<h3 id="Backward_compatibility" name="Backward_compatibility"> Backward compatibility </h3>
<h4 id="JavaScript_1.2_and_earlier_versions" name="JavaScript_1.2_and_earlier_versions"> JavaScript 1.2 and earlier versions </h4>
<p>The <code>Boolean</code> object behaves as follows:
</p>
<ul><li> When a <code>Boolean</code> object is used as the condition in a conditional test, JavaScript returns the value of the <code>Boolean</code> object. For example, a <code>Boolean</code> object whose value is false is treated as the primitive value false, and a <code>Boolean</code> object whose value is true is treated as the primitive value <code>true</code> in conditional tests. If the <code>Boolean</code> object is a <code>false</code> object, the conditional statement evaluates to <code>false</code>.
</li></ul>
<ul><li> You can use a Boolean object in place of a Boolean primitive.
</li></ul>
<h3 id="Properties" name="Properties"> Properties </h3>
<p><a href="cn/Core_JavaScript_1.5_Reference/Global_Objects/Boolean/constructor">constructor</a>: Specifies the function that creates an object's prototype.
</p><p><a href="cn/Core_JavaScript_1.5_Reference/Global_Objects/Boolean/prototype">prototype</a>: Defines a property that is shared by all Boolean objects.
</p>
<h3 id="Methods" name="Methods"> Methods </h3>
<p><a href="cn/Core_JavaScript_1.5_Reference/Global_Objects/Boolean/toSource">toSource</a>: Returns an object literal representing the specified Boolean object; you can use this value to create a new object. Overrides the <a href="cn/Core_JavaScript_1.5_Reference/Global_Objects/Object/toSource">Object.toSource</a> method.
</p><p><a href="cn/Core_JavaScript_1.5_Reference/Global_Objects/Boolean/toString">toString</a>: Returns a string representing the specified object. Overrides the <a href="cn/Core_JavaScript_1.5_Reference/Global_Objects/Object/toString">Object.toString</a> method.
</p><p><a href="cn/Core_JavaScript_1.5_Reference/Global_Objects/Boolean/valueOf">valueOf</a>: Returns the primitive value of a Boolean object. Overrides the <a href="cn/Core_JavaScript_1.5_Reference/Global_Objects/Object/valueOf">Object.valueOf</a> method.
</p><p>In addition, this object inherits the <a href="cn/Core_JavaScript_1.5_Reference/Global_Objects/Object/watch">watch</a> and <a href="cn/Core_JavaScript_1.5_Reference/Global_Objects/Object/unwatch">unwatch</a> methods from <a href="cn/Core_JavaScript_1.5_Reference/Global_Objects/Object">Object</a>.
</p>
<h3 id="Examples" name="Examples"> Examples </h3>
<h4 id="Creating_Boolean_objects_with_an_initial_value_of_false" name="Creating_Boolean_objects_with_an_initial_value_of_false"> Creating <code>Boolean</code> objects with an initial value of false </h4>
<pre class="eval">bNoParam = new Boolean();
bZero = new Boolean(0);
bNull = new Boolean(null);
bEmptyString = new Boolean("");
bfalse = new Boolean(false);
</pre>
<h4 id="Creating_Boolean_objects_with_an_initial_value_of_true" name="Creating_Boolean_objects_with_an_initial_value_of_true"> Creating <code>Boolean</code> objects with an initial value of true </h4>
<pre class="eval">btrue = new Boolean(true);
btrueString = new Boolean("true");
bfalseString = new Boolean("false");
bSuLin = new Boolean("Su Lin");
</pre>{{ languages( { "fr": "fr/R\u00e9f\u00e9rence_de_JavaScript_1.5_Core/Objets_globaux/Boolean", "ja": "ja/Core_JavaScript_1.5_Reference/Global_Objects/Boolean", "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Obiekty/Boolean" } ) }}
恢复到这个版本