aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/global_objects/boolean
diff options
context:
space:
mode:
authorPeter Bengtsson <mail@peterbe.com>2020-12-08 14:40:17 -0500
committerPeter Bengtsson <mail@peterbe.com>2020-12-08 14:40:17 -0500
commit33058f2b292b3a581333bdfb21b8f671898c5060 (patch)
tree51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/javascript/reference/global_objects/boolean
parent8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff)
downloadtranslated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz
translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2
translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip
initial commit
Diffstat (limited to 'files/zh-cn/web/javascript/reference/global_objects/boolean')
-rw-r--r--files/zh-cn/web/javascript/reference/global_objects/boolean/index.html120
-rw-r--r--files/zh-cn/web/javascript/reference/global_objects/boolean/prototype/index.html75
-rw-r--r--files/zh-cn/web/javascript/reference/global_objects/boolean/tosource/index.html59
-rw-r--r--files/zh-cn/web/javascript/reference/global_objects/boolean/tostring/index.html87
-rw-r--r--files/zh-cn/web/javascript/reference/global_objects/boolean/valueof/index.html83
5 files changed, 424 insertions, 0 deletions
diff --git a/files/zh-cn/web/javascript/reference/global_objects/boolean/index.html b/files/zh-cn/web/javascript/reference/global_objects/boolean/index.html
new file mode 100644
index 0000000000..66b0fb1417
--- /dev/null
+++ b/files/zh-cn/web/javascript/reference/global_objects/boolean/index.html
@@ -0,0 +1,120 @@
+---
+title: Boolean
+slug: Web/JavaScript/Reference/Global_Objects/Boolean
+tags:
+ - Boolean
+ - Constructor
+ - JavaScript
+translation_of: Web/JavaScript/Reference/Global_Objects/Boolean
+---
+<p>{{JSRef}}</p>
+
+<p><strong><code>Boolean</code></strong>对象是一个布尔值的对象包装器。</p>
+
+<h2 id="描述">描述</h2>
+
+<p>如果需要,作为第一个参数传递的值将转换为布尔值。如果省略或值<code>0</code>,<code>-0</code>,{{jsxref("null")}},<code>false</code>,{{jsxref("NaN")}},{{jsxref("undefined")}},或空字符串(<code>""</code>),该对象具有的初始值<code>false</code>。所有其他值,包括任何对象,空数组(<code>[]</code>)或字符串<code>"false"</code>,都会创建一个初始值为<code>true</code>的对象。</p>
+
+<p>注意不要将基本类型中的布尔值 <code>true</code> 和 <code>false</code> 与值为 <code>true</code> 和 <code>false</code> 的 <code>Boolean</code> 对象弄混了。</p>
+
+<p>其值不是{{jsxref("undefined")}}或{{jsxref("null")}}的任何对象(包括其值为<code>false</code>的布尔对象)在传递给条件语句时都将计算为<code>true</code>。 例如,以下{{jsxref("Statements/if...else", "if")}}语句中的条件评估为<code>true</code>:</p>
+
+<pre class="brush: js notranslate">var x = new Boolean(false);
+if (x) {
+ // 这里的代码会被执行
+}
+</pre>
+
+<p>基本类型的布尔值不受此规则影响。例如下面的 {{jsxref("Statements/if...else", "if")}} 语句的条件为假:</p>
+
+<pre class="brush: js notranslate">var x = false;
+if (x) {
+ // 这里的代码不会执行
+}
+</pre>
+
+<p>不要用创建 <code>Boolean</code> 对象的方式将一个非布尔值转化成布尔值,直接将 <code>Boolean</code> 当做转换函数来使用即可,或者使用<a href="/zh-CN/docs/Web/JavaScript/Reference/Operators/Logical_Operators#%E9%80%BB%E8%BE%91%E9%9D%9E%EF%BC%88!%EF%BC%89">双重非(!!)运算符</a>:</p>
+
+<pre class="brush: js notranslate">var x = Boolean(expression); // 推荐
+var x = !!(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 notranslate">var myFalse = new Boolean(false); // true
+var g = new Boolean(myFalse); // true
+var myString = new String("Hello");
+var s = new Boolean(myString); // true
+</pre>
+
+<p>最后,不要在应该使用基本类型布尔值的地方使用 <code>Boolean</code> 对象。</p>
+
+<div class="blockIndicator note">
+<p><strong>注意:</strong>当将非标准属性<a href="https://wiki.developer.mozilla.org/en-US/docs/Web/API/Document#Properties">document.all</a>用作此构造函数的参数时,结果是值为<code>false</code>的布尔对象。 此属性是旧属性,是非标准属性,不应使用。</p>
+</div>
+
+<h2 id="构造器">构造器</h2>
+
+<dl>
+ <dt><a href="https://wiki.developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean/Boolean"><code>Boolean()</code></a></dt>
+ <dd>创建一个新的<code>Boolean</code> 对象。</dd>
+</dl>
+
+<h2 id="实例方法">实例方法</h2>
+
+<dl>
+ <dt>{{jsxref("Boolean.prototype.toString()")}}</dt>
+ <dd>根据对象的值返回字符串<code>"true"</code>或<code>"false"</code>。 重写{{jsxref("Object.prototype.toString()")}}方法。</dd>
+ <dt>{{jsxref("Boolean.prototype.valueOf()")}}</dt>
+ <dd>返回{{jsxref("Boolean")}}对象的原始值。 重写{{jsxref("Object.prototype.valueOf()")}}方法。</dd>
+</dl>
+
+<h2 id="示例">示例</h2>
+
+<h3 id="创建值为_false_的_Boolean_对象">创建值为 <code>false</code> 的 <code>Boolean</code> 对象</h3>
+
+<pre class="brush: js notranslate">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 notranslate">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>
+
+<table class="standard-table">
+ <tbody>
+ <tr>
+ <td>规范</td>
+ </tr>
+ <tr>
+ <td>{{SpecName('ESDraft', '#sec-boolean-objects', 'Boolean')}}</td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="浏览器兼容性"><br>
+ 浏览器兼容性</h2>
+
+<div class="hidden">The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> and send us a pull request.</div>
+
+<p>{{Compat("javascript.builtins.Boolean")}}</p>
+
+<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>
diff --git a/files/zh-cn/web/javascript/reference/global_objects/boolean/prototype/index.html b/files/zh-cn/web/javascript/reference/global_objects/boolean/prototype/index.html
new file mode 100644
index 0000000000..a29fd8eb9a
--- /dev/null
+++ b/files/zh-cn/web/javascript/reference/global_objects/boolean/prototype/index.html
@@ -0,0 +1,75 @@
+---
+title: Boolean.prototype
+slug: Web/JavaScript/Reference/Global_Objects/Boolean/prototype
+tags:
+ - Boolean
+ - JavaScript
+ - Property
+ - Prototype
+translation_of: Web/JavaScript/Reference/Global_Objects/Boolean
+---
+<p>{{JSRef}}</p>
+
+<p><strong><code>Boolean.prototype</code></strong> 属性表示{{jsxref("Boolean")}} 构造函数的原型。</p>
+
+<div>{{js_property_attributes(0,0,0)}}</div>
+
+<h2 id="Description" name="Description" style="margin-bottom: 20px; line-height: 30px;">描述</h2>
+
+<p>{{jsxref("Boolean")}}实例继承自<code>Boolean.prototype</code>。你可以使用构造函数的原型对象向所有{{jsxref("Boolean")}}实例添加属性或方法。</p>
+
+<h2 id="属性" style="margin-bottom: 20px; line-height: 30px;">属性</h2>
+
+<dl>
+ <dt><code>Boolean.prototype.constructor</code></dt>
+ <dd>返回创建了实例原型的函数。默认为{{jsxref("Boolean")}}函数。</dd>
+</dl>
+
+<h2 id="方法" style="margin-bottom: 20px; line-height: 30px;">方法</h2>
+
+<dl>
+ <dt>{{jsxref("Boolean.prototype.toSource()")}} {{ Non-standard_inline() }}</dt>
+ <dd>返回包含{{jsxref("Boolean")}}对象源码的字符串;你可以使用这个字符串来创建一个等价的对象。覆盖了{{jsxref("Object.prototype.toSource()")}} 方法。</dd>
+ <dt>{{jsxref("Boolean.prototype.toString()")}}</dt>
+ <dd>根据对象的值来返回一个字符串:<code>"true"</code> 或 <code>"false"</code>。覆盖了 {{jsxref("Object.prototype.toString()")}} 方法。</dd>
+ <dt>{{jsxref("Boolean.prototype.valueOf()")}}</dt>
+ <dd>返回{{jsxref("Boolean")}}对象的原始值。覆盖了 {{jsxref("Object.prototype.valueOf()")}} 方法。</dd>
+</dl>
+
+<h2 id="规范" style="margin-bottom: 20px; line-height: 30px;">规范</h2>
+
+<table class="standard-table">
+ <tbody>
+ <tr>
+ <th scope="col">Specification</th>
+ <th scope="col">Status</th>
+ <th scope="col">Comment</th>
+ </tr>
+ <tr>
+ <td>{{SpecName('ES1')}}</td>
+ <td>{{Spec2('ES1')}}</td>
+ <td>Initial definition. Implemented in JavaScript 1.0.</td>
+ </tr>
+ <tr>
+ <td>{{SpecName('ES5.1', '#sec-15.6.3.1', 'Boolean.prototype')}}</td>
+ <td>{{Spec2('ES5.1')}}</td>
+ <td> </td>
+ </tr>
+ <tr>
+ <td>{{SpecName('ES6', '#sec-boolean.prototype', 'Boolean.prototype')}}</td>
+ <td>{{Spec2('ES6')}}</td>
+ <td> </td>
+ </tr>
+ <tr>
+ <td>{{SpecName('ESDraft', '#sec-boolean.prototype', 'Boolean.prototype')}}</td>
+ <td>{{Spec2('ESDraft')}}</td>
+ <td> </td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="浏览器兼容" style="margin-bottom: 20px; line-height: 30px;">浏览器兼容</h2>
+
+<div class="hidden">The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> and send us a pull request.</div>
+
+<p>{{Compat("javascript.builtins.Boolean.prototype")}}</p>
diff --git a/files/zh-cn/web/javascript/reference/global_objects/boolean/tosource/index.html b/files/zh-cn/web/javascript/reference/global_objects/boolean/tosource/index.html
new file mode 100644
index 0000000000..2086ce07c6
--- /dev/null
+++ b/files/zh-cn/web/javascript/reference/global_objects/boolean/tosource/index.html
@@ -0,0 +1,59 @@
+---
+title: Boolean.prototype.toSource()
+slug: Web/JavaScript/Reference/Global_Objects/Boolean/toSource
+tags:
+ - JavaScript
+ - Method
+ - Prototype
+ - 布尔
+translation_of: Web/JavaScript/Reference/Global_Objects/Boolean/toSource
+---
+<div>{{JSRef}} {{non-standard_header}}</div>
+
+<p><span class="short_text" id="result_box" lang="zh-CN"><span>toSource()方法返回一个表示对象的源码的字符串。</span></span></p>
+
+<h2 id="语法">语法</h2>
+
+<pre class="syntaxbox"><var>booleanObj</var>.toSource()
+Boolean.toSource()</pre>
+
+<h3 id="返回值">返回值</h3>
+
+<p><span class="short_text" id="result_box" lang="zh-CN"><span>表示对象的源码的字符串。</span></span></p>
+
+<h2 id="描述">描述</h2>
+
+<p><span class="short_text" id="result_box" lang="zh-CN"><span>toSource方法返回以下值:</span></span></p>
+
+<ul>
+ <li><span id="result_box" lang="zh-CN"><span>对于内置的</span></span> {{jsxref("Boolean")}} <span lang="zh-CN"><span>对象,toSource返回以下字符串,表示源代码不可用:</span></span>
+
+ <pre class="brush: js">function Boolean() {
+ [native code]
+}
+</pre>
+ </li>
+ <li><span class="short_text" id="result_box" lang="zh-CN"><span>对于</span></span> {{jsxref("Boolean")}}<span class="short_text" lang="zh-CN"><span> 的实例,toSource返回一个表示源代码的字符串。</span></span></li>
+</ul>
+
+<p><span class="short_text" id="result_box" lang="zh-CN"><span>此方法通常由JavaScript在内部调用,而不是在代码中显式调用。</span></span></p>
+
+<h2 id="规范"><span class="short_text" id="result_box" lang="zh-CN"><span>规范</span></span></h2>
+
+<p><span id="result_box" lang="zh-CN"><span>不是任何标准的一部分。</span> <span>在JavaScript 1.3中实现。</span></span></p>
+
+<h2 id="浏览器兼容性">浏览器兼容性</h2>
+
+<div>
+<div>
+
+
+<p>{{Compat("javascript.builtins.Boolean.toSource")}}</p>
+</div>
+</div>
+
+<h2 id="相关链接">相关链接</h2>
+
+<ul>
+ <li>{{jsxref("Object.prototype.toSource()")}} {{non-standard_inline}}</li>
+</ul>
diff --git a/files/zh-cn/web/javascript/reference/global_objects/boolean/tostring/index.html b/files/zh-cn/web/javascript/reference/global_objects/boolean/tostring/index.html
new file mode 100644
index 0000000000..6245e953dc
--- /dev/null
+++ b/files/zh-cn/web/javascript/reference/global_objects/boolean/tostring/index.html
@@ -0,0 +1,87 @@
+---
+title: Boolean.prototype.toString()
+slug: Web/JavaScript/Reference/Global_Objects/Boolean/toString
+tags:
+ - Boolean
+ - JavaScript
+ - Method
+ - Prototype
+translation_of: Web/JavaScript/Reference/Global_Objects/Boolean/toString
+---
+<div>{{JSRef}}</div>
+
+<p><code><strong>toString()</strong></code> 方法返回指定的布尔对象的字符串形式。</p>
+
+<div>{{EmbedInteractiveExample("pages/js/boolean-tostring.html")}}</div>
+
+
+
+<h2 id="Syntax" name="Syntax">语法</h2>
+
+<pre class="syntaxbox"><var>bool</var>.toString()</pre>
+
+<h3 id="返回值">返回值</h3>
+
+<p>表示特定{{jsxref("Boolean")}}对象的字符串。</p>
+
+<h2 id="Description" name="Description">描述</h2>
+
+<p>{{jsxref("Boolean")}} 对象覆盖了 {{jsxref("Object")}} 对象的  <code>toString</code> 方法。并没有继承 {{jsxref("Object.prototype.toString()")}}。对于布尔对象,<code>toString</code> 方法返回该对象的字符串形式。</p>
+
+<p>当一个{{jsxref("Boolean")}}对象作为文本值或进行字符串连接时,JavaScript 会自动调用其 <code>toString</code> 方法。</p>
+
+<p>对于{{jsxref("Boolean")}}对象或值,内置的 <code>toString</code> 方法返回字符串 "<code>true</code>" 或 "<code>false</code>",具体返回哪个取决于布尔对象的值。</p>
+
+<h2 id="示例">示例</h2>
+
+<h3 id="使用_toString">使用 <code>toString</code></h3>
+
+<p>下面的代码,<code>flag.toString</code> 返回 "<code>true</code>":</p>
+
+<pre class="brush: js">var flag = new Boolean(true)
+var myVar = flag.toString()
+</pre>
+
+<h2 id="规范">规范</h2>
+
+<table class="standard-table">
+ <tbody>
+ <tr>
+ <th scope="col">Specification</th>
+ <th scope="col">Status</th>
+ <th scope="col">Comment</th>
+ </tr>
+ <tr>
+ <td>{{SpecName('ES1')}}</td>
+ <td>{{Spec2('ES1')}}</td>
+ <td>Initial definition.</td>
+ </tr>
+ <tr>
+ <td>{{SpecName('ES5.1', '#sec-15.6.4.2', 'Boolean.prototype.toString')}}</td>
+ <td>{{Spec2('ES5.1')}}</td>
+ <td> </td>
+ </tr>
+ <tr>
+ <td>{{SpecName('ES6', '#sec-boolean.prototype.tostring', 'Boolean.prototype.toString')}}</td>
+ <td>{{Spec2('ES6')}}</td>
+ <td> </td>
+ </tr>
+ <tr>
+ <td>{{SpecName('ESDraft', '#sec-boolean.prototype.tostring', 'Boolean.prototype.toString')}}</td>
+ <td>{{Spec2('ESDraft')}}</td>
+ <td> </td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="浏览器兼容">浏览器兼容</h2>
+
+<div class="hidden">The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> and send us a pull request.</div>
+
+<p>{{Compat("javascript.builtins.Boolean.toString")}}</p>
+
+<h2 id="See_Also" name="See_Also">相关链接</h2>
+
+<ul>
+ <li>{{jsxref("Object.prototype.toString()")}}</li>
+</ul>
diff --git a/files/zh-cn/web/javascript/reference/global_objects/boolean/valueof/index.html b/files/zh-cn/web/javascript/reference/global_objects/boolean/valueof/index.html
new file mode 100644
index 0000000000..068e594b92
--- /dev/null
+++ b/files/zh-cn/web/javascript/reference/global_objects/boolean/valueof/index.html
@@ -0,0 +1,83 @@
+---
+title: Boolean.prototype.valueOf()
+slug: Web/JavaScript/Reference/Global_Objects/Boolean/valueOf
+tags:
+ - Boolean
+ - JavaScript
+ - Method
+ - Prototype
+translation_of: Web/JavaScript/Reference/Global_Objects/Boolean/valueOf
+---
+<div>{{JSRef}}</div>
+
+<p><code><strong>valueOf()</strong></code> 方法返回一个{{jsxref("Boolean")}}对象的原始值。</p>
+
+<div>{{EmbedInteractiveExample("pages/js/boolean-valueof.html")}}</div>
+
+
+
+<h2 id="Syntax" name="Syntax">语法</h2>
+
+<pre class="syntaxbox"><code><var>bool</var>.valueOf()</code></pre>
+
+<h3 id="返回值">返回值</h3>
+
+<p>给定{{jsxref("Boolean")}}对象的原始值</p>
+
+<h2 id="Description" name="Description">描述</h2>
+
+<p>{{jsxref("Boolean")}}的 <code>valueOf</code> 方法返回一个{{jsxref("Boolean")}}对象或{{jsxref("Boolean")}}字面量的原始值作为布尔数据类型。</p>
+
+<p>该方法通常在 JavaScript 内部调用,而不是在代码中显式调用。</p>
+
+<h2 id="Examples" name="Examples">示例</h2>
+
+<h3 id="Example:_Using_valueOf" name="Example:_Using_valueOf">使用 <code>valueOf</code></h3>
+
+<pre class="brush: js">x = new Boolean();
+myVar = x.valueOf() // assigns false to myVar
+</pre>
+
+<h2 id="规范">规范</h2>
+
+<table class="standard-table">
+ <tbody>
+ <tr>
+ <th scope="col">Specification</th>
+ <th scope="col">Status</th>
+ <th scope="col">Comment</th>
+ </tr>
+ <tr>
+ <td>{{SpecName('ES1')}}</td>
+ <td>{{Spec2('ES1')}}</td>
+ <td>Initial definition. Implemented in JavaScript 1.1.</td>
+ </tr>
+ <tr>
+ <td>{{SpecName('ES5.1', '#sec-15.6.4.3', 'Boolean.prototype.valueOf')}}</td>
+ <td>{{Spec2('ES5.1')}}</td>
+ <td> </td>
+ </tr>
+ <tr>
+ <td>{{SpecName('ES6', '#sec-boolean.prototype.valueof', 'Boolean.prototype.valueOf')}}</td>
+ <td>{{Spec2('ES6')}}</td>
+ <td> </td>
+ </tr>
+ <tr>
+ <td>{{SpecName('ESDraft', '#sec-boolean.prototype.valueof', 'Boolean.prototype.valueOf')}}</td>
+ <td>{{Spec2('ESDraft')}}</td>
+ <td> </td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="浏览器兼容">浏览器兼容</h2>
+
+<div class="hidden">The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> and send us a pull request.</div>
+
+<p>{{Compat("javascript.builtins.Boolean.valueOf")}}</p>
+
+<h2 id="See_Also" name="See_Also">相关链接</h2>
+
+<ul>
+ <li>{{jsxref("Object.prototype.valueOf()")}}</li>
+</ul>