diff options
author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
---|---|---|
committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
commit | 33058f2b292b3a581333bdfb21b8f671898c5060 (patch) | |
tree | 51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/javascript/reference/global_objects/object/tostring | |
parent | 8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff) | |
download | translated-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/object/tostring')
-rw-r--r-- | files/zh-cn/web/javascript/reference/global_objects/object/tostring/index.html | 139 |
1 files changed, 139 insertions, 0 deletions
diff --git a/files/zh-cn/web/javascript/reference/global_objects/object/tostring/index.html b/files/zh-cn/web/javascript/reference/global_objects/object/tostring/index.html new file mode 100644 index 0000000000..a38a5d8913 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/object/tostring/index.html @@ -0,0 +1,139 @@ +--- +title: Object.prototype.toString() +slug: Web/JavaScript/Reference/Global_Objects/Object/toString +tags: + - JavaScript + - Method + - Object + - Prototype + - 原型 + - 对象 + - 方法 +translation_of: Web/JavaScript/Reference/Global_Objects/Object/toString +--- +<p>{{JSRef}}</p> + +<p><code><strong>toString()</strong></code> 方法返回一个表示该对象的字符串。</p> + +<div>{{EmbedInteractiveExample("pages/js/object-prototype-tostring.html")}}</div> + + + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox"><code><var>obj</var>.toString()</code></pre> + +<h3 id="返回值">返回值</h3> + +<p>一个表示该对象的字符串。</p> + +<h2 id="描述">描述</h2> + +<p>每个对象都有一个 <code>toString()</code> 方法,当该对象被表示为一个文本值时,或者一个对象以预期的字符串方式引用时自动调用。默认情况下,<code>toString()</code> 方法被每个 <code>Object</code> 对象继承。如果此方法在自定义对象中未被覆盖,<code>toString()</code> 返回 "[object <em>type</em>]",其中 <code>type</code> 是对象的类型。以下代码说明了这一点:</p> + +<pre class="brush: js">var o = new Object(); +o.toString(); // returns [object Object] +</pre> + +<div class="note"><strong>注意:</strong>如的ECMAScript 5 和随后的 Errata 中所定义,从 JavaScript 1.8.5 开始,<code>toString()</code> 调用 {{jsxref("null")}} 返回<code>[object <em>Null</em>]</code>,{{jsxref("undefined")}} 返回 <code>[object Undefined]</code>。请参阅下面的{{anch("Using_toString()_to_detect_object_class", "使用 <code>toString()</code> 检测对象类型")}}。</div> + +<h2 id="示例">示例</h2> + +<h3 id="覆盖默认的_toString_方法">覆盖默认的 <code>toString</code> 方法</h3> + +<p>可以自定义一个方法,来取代默认的 <code>toString()</code> 方法。该 <code>toString()</code> 方法不能传入参数,并且必须返回一个字符串。自定义的 <code>toString()</code><span> 方法可以是任何我们需要的值,但如果它附带有关对象的信息,它将变得非常有用。</span></p> + +<p>以下代码定义了 <code>Dog</code> 对象类型,并创建了一个 <code>Dog</code> 类型的 <code>theDog</code> 对象:</p> + +<pre class="brush: js">function Dog(name,breed,color,sex) { + this.name = name; + this.breed = breed; + this.color = color; + this.sex = sex; +} + +var theDog = new Dog("Gabby", "Lab", "chocolate", "female");</pre> + +<p>如果当前的对象调用了 <code>toString()</code> 方法,它将会返回从 {{jsxref("Object")}}继承而来的 <code>toString()</code> 方法的返回默认值:</p> + +<pre class="brush: js"><code>theDog.toString(); // 返回 [object Object]</code></pre> + +<p>下面的代码中定义了一个叫做 <code>dogToString()</code> 的方法来覆盖默认的 <code>toString()</code> 方法。这个方法生成一个 "<code>property = value;</code>" 形式的字符串,该字符串包含了当前对象的 name、breed、color 和 sex 的值。</p> + +<pre class="brush: js">Dog.prototype.toString = function dogToString() { + var ret = "Dog " + this.name + " is a " + this.sex + " " + this.color + " " + this.breed; + return ret; +}</pre> + +<p>也可以这样写</p> + +<pre class="brush: js">Dog.prototype.toString = function dogToString() { + return `Dog ${this.name} is a ${this.sex} ${this.color} ${this.breed}`; +} +</pre> + +<p>使用上述代码,任何时候在字符串上下文中使用 <code>theDog.toString()</code> 时,JavaScript 都会自动调用 <code>dogToString()</code> 方法(<code>dogToString()</code> 可以是一个匿名函数),并且返回以下字符串:</p> + +<pre class="brush: js"><code>"Dog Gabby is a female chocolate Lab"</code></pre> + +<h3 id="使用_toString()_检测对象类型">使用 <code>toString()</code> 检测对象类型</h3> + +<p>可以通过 <code>toString()</code> 来获取每个对象的类型。为了每个对象都能通过 <code>Object.prototype.toString()</code> 来检测,需要以 <code>Function.prototype.call()</code> 或者 <code>Function.prototype.apply()</code> 的形式来调用,传递要检查的对象作为第一个参数,称为 <code>thisArg</code>。</p> + +<pre class="brush: js">var toString = Object.prototype.toString; + +toString.call(new Date); // [object Date] +toString.call(new String); // [object String] +toString.call(Math); // [object Math] + +//Since JavaScript 1.8.5 +toString.call(undefined); // [object Undefined] +toString.call(null); // [object Null] +</pre> + +<h2 id="规范">规范</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">规范</th> + <th scope="col">状态</th> + <th scope="col">备注</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.2.4.2', 'Object.prototype.toString')}}</td> + <td>{{Spec2('ES5.1')}}</td> + <td>Call on {{jsxref("null")}} returns <code>[object <em>Null</em>]</code>, and {{jsxref("undefined")}} returns <code>[object <em>Undefined</em>]</code></td> + </tr> + <tr> + <td>{{SpecName('ES6', '#sec-object.prototype.tostring', 'Object.prototype.toString')}}</td> + <td>{{Spec2('ES6')}}</td> + <td></td> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-object.prototype.tostring', 'Object.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.Object.toString")}}</p> + +<h2 id="参见">参见</h2> + +<ul> + <li>{{jsxref("Object.prototype.toSource()")}}</li> + <li>{{jsxref("Object.prototype.valueOf()")}}</li> + <li>{{jsxref("Number.prototype.toString()")}}</li> + <li>{{jsxref("Symbol.toPrimitive")}}</li> +</ul> |