aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/global_objects/object/tostring/index.html
blob: a38a5d8913d48db39eda65a4527be5f5e332af61 (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
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>