mirror of
https://github.com/fofolee/uTools-Manuals.git
synced 2025-06-12 09:44:04 +08:00
136 lines
5.6 KiB
HTML
136 lines
5.6 KiB
HTML
<article id="wikiArticle">
|
||
<div>
|
||
</div>
|
||
<h2 id="Summary" name="Summary">概述</h2>
|
||
<p><code><strong>toString()</strong></code> 方法返回一个指定的错误对象(Error object)的字符串表示。</p>
|
||
<h2 id="Syntax" name="Syntax">语法</h2>
|
||
<pre><code class="language-javascript"><code><em>e</em>.toString();</code></code></pre>
|
||
<h2 id="Description" name="Description">描述</h2>
|
||
<p><a href="Reference/Global_Objects/Error" title="通过Error的构造器可以创建一个错误对象。当运行时错误产生时,Error的实例对象会被抛出。Error对象也可用于用户自定义的异常的基础对象。下面列出了各种内建的标准错误类型。"><code>Error</code></a> 对象覆盖了 <a href="Reference/Global_Objects/Object/toString" title="toString() 方法返回一个表示该对象的字符串。"><code>Object.prototype.toString()</code></a> 方法。该方法实现如下:(假定 <code>Object</code> 和 <code>String</code> 没有被更改):</p>
|
||
<pre><code class="language-js">Error.prototype.toString = function()
|
||
{
|
||
"use strict";
|
||
|
||
var obj = Object(this);
|
||
if (obj !== this)
|
||
throw new TypeError();
|
||
|
||
var name = this.name;
|
||
name = (name === undefined) ? "Error" : String(name);
|
||
|
||
var msg = this.message;
|
||
msg = (msg === undefined) ? "" : String(msg);
|
||
|
||
if (name === "")
|
||
return msg;
|
||
if (msg === "")
|
||
return name;
|
||
|
||
return name + ": " + msg;
|
||
};
|
||
</code></pre>
|
||
<h2 id="Example" name="Example">示例</h2>
|
||
<pre><code class="language-js">var e = new Error("fatal error");
|
||
print(e.toString()); // "Error: fatal error"
|
||
|
||
e.name = undefined;
|
||
print(e.toString()); // "Error: fatal error"
|
||
|
||
e.name = "";
|
||
print(e.toString()); // "fatal error"
|
||
|
||
e.message = undefined;
|
||
print(e.toString()); // "Error"
|
||
|
||
e.name = "hello";
|
||
print(e.toString()); // "hello"
|
||
</code></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>ECMAScript 1st Edition.</td>
|
||
<td>Standard</td>
|
||
<td>Initial definition.<br/>
|
||
Implemented in JavaScript 1.1</td>
|
||
</tr>
|
||
<tr>
|
||
<td><a class="external" href="https://www.ecma-international.org/ecma-262/5.1/#sec-15.11.4.4" hreflang="en" lang="en" rel="noopener">ECMAScript 5.1 (ECMA-262)<br/><small lang="zh-CN">Error.prototype.toString</small></a></td>
|
||
<td><span class="spec-Standard">Standard</span></td>
|
||
<td> </td>
|
||
</tr>
|
||
<tr>
|
||
<td><a class="external" href="https://www.ecma-international.org/ecma-262/6.0/#sec-error.prototype.tostring" hreflang="en" lang="en" rel="noopener">ECMAScript 2015 (6th Edition, ECMA-262)<br/><small lang="zh-CN">Error.prototype.toString</small></a></td>
|
||
<td><span class="spec-Standard">Standard</span></td>
|
||
<td> </td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
<h2 id="浏览器兼容性">浏览器兼容性</h2>
|
||
<p></p><div class="blockIndicator warning"><strong><a class="external" href="https://github.com/mdn/browser-compat-data" rel="noopener">We're converting our compatibility data into a machine-readable JSON format</a></strong>.
|
||
This compatibility table still uses the old format,
|
||
because we haven't yet converted the data it contains.
|
||
<strong><a class="new" href="/zh-CN/docs/MDN/Contribute/Structures/Compatibility_tables" rel="nofollow">Find out how you can help!</a></strong></div>
|
||
<div class="htab">
|
||
<a id="AutoCompatibilityTable" name="AutoCompatibilityTable"></a>
|
||
<ul>
|
||
<li class="selected"><a>Desktop</a></li>
|
||
<li><a>Mobile</a></li>
|
||
</ul>
|
||
</div><p></p>
|
||
<div id="compat-desktop">
|
||
<table class="compat-table">
|
||
<tbody>
|
||
<tr>
|
||
<th>Feature</th>
|
||
<th>Chrome</th>
|
||
<th>Firefox (Gecko)</th>
|
||
<th>Internet Explorer</th>
|
||
<th>Opera</th>
|
||
<th>Safari</th>
|
||
</tr>
|
||
<tr>
|
||
<td>Basic support</td>
|
||
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
|
||
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
|
||
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
|
||
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
|
||
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
</div>
|
||
<div id="compat-mobile">
|
||
<table class="compat-table">
|
||
<tbody>
|
||
<tr>
|
||
<th>Feature</th>
|
||
<th>Android</th>
|
||
<th>Chrome for Android</th>
|
||
<th>Firefox Mobile (Gecko)</th>
|
||
<th>IE Mobile</th>
|
||
<th>Opera Mobile</th>
|
||
<th>Safari Mobile</th>
|
||
</tr>
|
||
<tr>
|
||
<td>Basic support</td>
|
||
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
|
||
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
|
||
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
|
||
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
|
||
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
|
||
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
</div>
|
||
<h2 id="See_also" name="See_also">相关链接</h2>
|
||
<ul>
|
||
<li><a href="Reference/Global_Objects/Error/toSource" title="toSource() 方法返回可以计算出到相同错误的代码。"><code>Error.prototype.toSource()</code></a></li>
|
||
</ul>
|
||
</article> |