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/error | |
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/error')
10 files changed, 1166 insertions, 0 deletions
diff --git a/files/zh-cn/web/javascript/reference/global_objects/error/columnnumber/index.html b/files/zh-cn/web/javascript/reference/global_objects/error/columnnumber/index.html new file mode 100644 index 0000000000..cff8e92402 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/error/columnnumber/index.html @@ -0,0 +1,81 @@ +--- +title: Error.prototype.columnNumber +slug: Web/JavaScript/Reference/Global_Objects/Error/columnNumber +translation_of: Web/JavaScript/Reference/Global_Objects/Error/columnNumber +--- +<div>{{JSRef}} {{non-standard_header}}</div> + +<p><code><strong>columnNumber</strong></code>属性包含引发此错误的文件行中的列号。</p> + +<h2 id="例子">例子</h2> + +<h3 id="使用_columnNumber">使用 <code>columnNumber</code></h3> + +<pre class="brush: js">var e = new Error('Could not parse input'); +throw e; +console.log(e.columnNumber) // 0 +</pre> + +<h2 id="规范">规范</h2> + +<p>不属于任何规范的一部分. 非标准.</p> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<div>{{CompatibilityTable}}</div> + +<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>基本支持</td> + <td>{{CompatNo}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</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>基本支持</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="相关链接">相关链接</h2> + +<ul> + <li>{{jsxref("Error.prototype.stack")}} {{non-standard_inline}}</li> + <li>{{jsxref("Error.prototype.lineNumber")}} {{non-standard_inline}}</li> + <li>{{jsxref("Error.prototype.fileName")}} {{non-standard_inline}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/global_objects/error/filename/index.html b/files/zh-cn/web/javascript/reference/global_objects/error/filename/index.html new file mode 100644 index 0000000000..430cb738c2 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/error/filename/index.html @@ -0,0 +1,85 @@ +--- +title: Error.prototype.fileName +slug: Web/JavaScript/Reference/Global_Objects/Error/fileName +translation_of: Web/JavaScript/Reference/Global_Objects/Error/fileName +--- +<div>{{JSRef}} {{non-standard_header}}</div> + +<p><code><strong>fileName</strong></code> 属性包含引发此错误的文件的路径.</p> + +<h2 id="描述">描述</h2> + +<p>此非标准属性包含引发此错误的文件的路径. 如果从调试器上下文调用,例如Firefox Developer Tools,将会返回“debugger eval code”.</p> + +<h2 id="例子">例子</h2> + +<h3 id="使用_fileName">使用 <code>fileName</code></h3> + +<pre class="brush: js">var e = new Error('Could not parse input'); +throw e; +// e.fileName could look like "file:///C:/example.html" +</pre> + +<h2 id="规范">规范</h2> + +<p>不属于任何规范的一部分. 非标准.</p> + +<h2 id="Browser_compatibility">Browser compatibility</h2> + +<div>{{CompatibilityTable}}</div> + +<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>基本支持</td> + <td>{{CompatNo}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</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>基本支持</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="相关链接">相关链接</h2> + +<ul> + <li>{{jsxref("Error.prototype.stack")}} {{non-standard_inline}}</li> + <li>{{jsxref("Error.prototype.columnNumber")}} {{non-standard_inline}}</li> + <li>{{jsxref("Error.prototype.lineNumber")}} {{non-standard_inline}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/global_objects/error/index.html b/files/zh-cn/web/javascript/reference/global_objects/error/index.html new file mode 100644 index 0000000000..27bd521d4e --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/error/index.html @@ -0,0 +1,257 @@ +--- +title: Error +slug: Web/JavaScript/Reference/Global_Objects/Error +tags: + - Error + - Whoops! + - 参考 +translation_of: Web/JavaScript/Reference/Global_Objects/Error +--- +<p>{{JSRef}}</p> + +<p><span style="line-height: inherit;">通过<strong>Error</strong>的构造器可以创建一个错误对象。当运行时错误产生时,Error的实例对象会被抛出。Error对象</span>也<span style="line-height: inherit;">可用于用户自定义的异常的基础对象。下面列出了各种内建的标准错误类型。</span></p> + +<h2 id="Syntax" name="Syntax">语法</h2> + +<pre class="brush: js">new Error([<em>message</em>[, <em>fileName</em>[,<em>lineNumber</em>]]])</pre> + +<h3 id="Parameters" name="Parameters">参数</h3> + +<dl> + <dt><code>message</code></dt> + <dd>可选。人类可阅读的错误描述信息。</dd> +</dl> + +<dl> + <dt><code>fileName </code>{{non-standard_inline}}</dt> + <dd>可选。被创建的<span style="font-family: monospace; line-height: inherit;">Error对象的fileName属性值。</span><span style="line-height: inherit;">默认是调用Error构造器代码所在的文件 的名字。</span></dd> +</dl> + +<dl> + <dt><code>lineNumber </code>{{non-standard_inline}}</dt> + <dd>可选。被创建的<span style="font-family: monospace; line-height: inherit;">Error对象的lineNumber属性值。默认是</span><span style="line-height: inherit;">调用Error构造器代码所在的文件的行号</span><span style="font-family: monospace; line-height: inherit;">。</span></dd> +</dl> + +<h2 id="Description" name="Description">描述</h2> + +<p>当代码运行时的发生错误,会创建新的<code>Error</code> 对象,并将其抛出。</p> + +<p><span style="line-height: inherit;">该页面描述了Error对象自身的使用,以及其构造函数的使用</span><span style="line-height: inherit;">. 关于Error实例的内部属性和方法,请看</span> {{jsxref("Error.prototype")}}。</p> + +<h3 id="作为函数使用">作为函数使用</h3> + +<p>当像函数一样使用 <code>Error</code> 时 -- 如果没有 {{jsxref("Operators/new", "new")}},它将返回一个 <code>Error</code> 对象。所以, 仅仅调用 <code>Error</code> 产生的结果与通过<code>new</code> 关键字构造 <code>Error</code> 对象生成的结果相同。 </p> + +<pre><code>// this: +const x = Error('I was created using a function call!'); +// has the same functionality as this: +const y = new Error('I was constructed via the "new" keyword!');</code></pre> + +<h3 id="Error_types" name="Error_types">Error 类型</h3> + +<p>除了通用的<span style="font-family: courier new,andale mono,monospace; line-height: inherit;">Error构造函数外,</span>JavaScript<span style="font-family: courier new,andale mono,monospace; line-height: inherit;">还有6个其他类型的错误构造函数。更多客户端异常,详见</span><span style="line-height: inherit;"> </span><a href="/en/JavaScript/Guide/Statements#Exception_Handling_Statements" style="line-height: inherit;" title="en/JavaScript/Guide/Statements#Exception Handling Statements">Exception Handling Statements</a>。</p> + +<dl> + <dt><strong style="font-weight: bold;">{{jsxref("EvalError")}}</strong></dt> + <dd>创建一个error实例,表示错误的原因:与 {{jsxref("Global_Objects/eval", "eval()")}} 有关。</dd> + <dt><strong style="font-weight: bold;">{{jsxref("InternalError")}} {{non-standard_inline}}</strong></dt> + <dd>创建一个代表Javascript引擎内部错误的异常抛出的实例。 如: "递归太多".</dd> +</dl> + +<dl> + <dt><strong style="font-weight: bold;">{{jsxref("RangeError", "RangeError")}}</strong></dt> + <dd>创建一个error实例,表示错误的原因:<span style="line-height: inherit;">数值变量或参数超出其有效范围</span><span style="line-height: inherit;">。</span></dd> +</dl> + +<dl> + <dt><strong style="font-weight: bold;">{{jsxref("ReferenceError")}}</strong></dt> + <dd>创建一个error实例,表示错误的原因:无效引用。</dd> +</dl> + +<dl> + <dt><strong style="font-weight: bold;">{{jsxref("SyntaxError")}}</strong></dt> + <dd>创建一个error实例,表示错误的原因:{{jsxref("Global_Objects/eval", "eval()")}}在解析代码的过程中发生的语法错误<span style="line-height: inherit;">。</span></dd> +</dl> + +<dl> + <dt><strong style="font-weight: bold;">{{jsxref("TypeError")}}</strong></dt> + <dd>创建一个error实例,表示错误的原因:变量或参数不属于有效类型。</dd> +</dl> + +<dl> + <dt><strong style="font-weight: bold;">{{jsxref("URIError")}}</strong></dt> + <dd>创建一个error实例,表示错误的原因:给 {{jsxref("Global_Objects/encodeURI", "encodeURI()")}}<span style="line-height: inherit;">或 </span> {{jsxref("Global_Objects/decodeURI", "decodeURl()")}}<span style="line-height: inherit;">传递的参数无效。</span></dd> +</dl> + +<h2 id="Properties" name="Properties">属性</h2> + +<dl> + <dt>{{jsxref("Error.prototype")}}</dt> + <dd>允许添加属性到<code>Error</code>实例。</dd> +</dl> + +<h2 id="Methods" name="Methods">方法</h2> + +<p>全局<code>Error</code>对象自身不包含任何方法,但从原型链中继承了一些方法.</p> + +<h2 id="Error_instances" name="Error_instances"><code>Error</code> 实例</h2> + +<div> +<p>{{page('en-US/docs/JavaScript/Reference/Global_Objects/Error/prototype', 'Description')}}</p> + +<h3 id="属性">属性</h3> + +<p>{{page('en-US/docs/JavaScript/Reference/Global_Objects/Error/prototype', 'Properties')}}</p> + +<h3 id="方法">方法</h3> + +<p>{{page('en-US/docs/JavaScript/Reference/Global_Objects/Error/prototype', 'Methods')}}</p> +</div> + +<h2 id="Examples" name="Examples">例子</h2> + +<h3 id="Example_Throwing_a_generic_error" name="Example:_Throwing_a_generic_error">抛出一个基本错误</h3> + +<p>通常你会使用{{jsxref("Statements/throw", "throw")}}关键字来抛出你创建的Error对象。可以使用 {{jsxref("Statements/try...catch", "try...catch")}} 结构来处理异常:</p> + +<pre class="brush: js">try { + throw new Error("Whoops!"); +} catch (e) { + alert(e.name + ": " + e.message); +} +</pre> + +<h3 id="Example_Handling_a_specific_error" name="Example:_Handling_a_specific_error">处理一个特定错误</h3> + +<p>你可以通过判断异常的类型来特定处理某一类的异常,即判断 {{jsxref("Object.prototype.constructor", "constructor")}} 属性,当使用现代Javascript引擎时,可使用{{jsxref("Operators/instanceof", "instanceof")}} 关键字:</p> + +<pre class="brush: js">try { + foo.bar(); +} catch (e) { + if (e instanceof EvalError) { + alert(e.name + ": " + e.message); + } else if (e instanceof RangeError) { + alert(e.name + ": " + e.message); + } + // ... etc +} +</pre> + +<h3 id="自定义异常类型">自定义异常类型</h3> + +<p>你可能希望自定义基于Error的异常类型,使得你能够 throw new MyError() 并可以使用 <code>instanceof MyError</code> 来检查某个异常的类型. 这种需求的通用解决方法如下.</p> + +<div class="warning" style="font-size: 14px;"> +<p>注意,在FireFox中抛出自定义类型的异常会显示不正确的行号和文件名。</p> +</div> + +<p>参考 <a href="http://stackoverflow.com/questions/1382107/whats-a-good-way-to-extend-error-in-javascript">"What's a good way to extend Error in JavaScript?" discussion on Stackoverflow</a>.</p> + +<pre class="brush: js">// Create a new object, that prototypally inherits from the Error constructor. +function MyError(message) { + this.name = 'MyError'; + this.message = message || 'Default Message'; + this.stack = (new Error()).stack; +} +MyError.prototype = Object.create(Error.prototype); +MyError.prototype.constructor = MyError; + +try { + throw new MyError(); +} catch (e) { + console.log(e.name); // 'MyError' + console.log(e.message); // 'Default Message' +} + +try { + throw new MyError('custom message'); +} catch (e) { + console.log(e.name); // 'MyError' + console.log(e.message); // 'custom message' +}</pre> + +<div class="line-number" style="margin-top: 1em; position: absolute; left: 0px; right: 0px; line-height: inherit; top: 0px; background: 0px 0px;"></div> + +<div class="line-number" style="margin-top: 1em; position: absolute; left: 0px; right: 0px; line-height: inherit; top: 19px; background: 0px 0px;"></div> + +<div class="line-number" style="margin-top: 1em; position: absolute; left: 0px; right: 0px; line-height: inherit; top: 38px; background: 0px 0px;"></div> + +<div class="line-number" style="margin-top: 1em; position: absolute; left: 0px; right: 0px; line-height: inherit; top: 57px; background: 0px 0px;"></div> + +<div class="line-number" style="margin-top: 1em; position: absolute; left: 0px; right: 0px; line-height: inherit; top: 76px; background: 0px 0px;"></div> + +<div class="line-number" style="margin-top: 1em; position: absolute; left: 0px; right: 0px; line-height: inherit; top: 95px; background: 0px 0px;"></div> + +<div class="line-number" style="margin-top: 1em; position: absolute; left: 0px; right: 0px; line-height: inherit; top: 114px; background: 0px 0px;"></div> + +<div class="line-number" style="margin-top: 1em; position: absolute; left: 0px; right: 0px; line-height: inherit; top: 133px; background: 0px 0px;"></div> + +<div class="line-number" style="margin-top: 1em; position: absolute; left: 0px; right: 0px; line-height: inherit; top: 152px; background: 0px 0px;"></div> + +<div class="line-number" style="margin-top: 1em; position: absolute; left: 0px; right: 0px; line-height: inherit; top: 171px; background: 0px 0px;"></div> + +<div class="line-number" style="margin-top: 1em; position: absolute; left: 0px; right: 0px; line-height: inherit; top: 190px; background: 0px 0px;"></div> + +<div class="line-number" style="margin-top: 1em; position: absolute; left: 0px; right: 0px; line-height: inherit; top: 209px; background: 0px 0px;"></div> + +<div class="line-number" style="margin-top: 1em; position: absolute; left: 0px; right: 0px; line-height: inherit; top: 228px; background: 0px 0px;"></div> + +<div class="line-number" style="margin-top: 1em; position: absolute; left: 0px; right: 0px; line-height: inherit; top: 247px; background: 0px 0px;"></div> + +<div class="line-number" style="margin-top: 1em; position: absolute; left: 0px; right: 0px; line-height: inherit; top: 266px; background: 0px 0px;"></div> + +<div class="line-number" style="margin-top: 1em; position: absolute; left: 0px; right: 0px; line-height: inherit; top: 285px; background: 0px 0px;"></div> + +<div class="line-number" style="margin-top: 1em; position: absolute; left: 0px; right: 0px; line-height: inherit; top: 304px; background: 0px 0px;"></div> + +<div class="line-number" style="margin-top: 1em; position: absolute; left: 0px; right: 0px; line-height: inherit; top: 323px; background: 0px 0px;"></div> + +<div class="line-number" style="margin-top: 1em; position: absolute; left: 0px; right: 0px; line-height: inherit; top: 342px; background: 0px 0px;"></div> + +<div class="line-number" style="margin-top: 1em; position: absolute; left: 0px; right: 0px; line-height: inherit; top: 361px; background: 0px 0px;"></div> + +<div class="line-number" style="margin-top: 1em; position: absolute; left: 0px; right: 0px; line-height: inherit; top: 380px; background: 0px 0px;"></div> + +<h2 id="规范" style="margin-bottom: 20px; line-height: 30px; font-size: 2.14285714285714rem;">规范</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>ECMAScript 1st Edition.</td> + <td>Standard</td> + <td>Initial definition. Implemented in JavaScript 1.1.</td> + </tr> + <tr> + <td>{{SpecName('ES5.1', '#sec-15.11', 'Error')}}</td> + <td>{{Spec2('ES5.1')}}</td> + <td></td> + </tr> + <tr> + <td>{{SpecName('ES6', '#sec-error-objects', 'Error')}}</td> + <td>{{Spec2('ES6')}}</td> + <td></td> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-error-objects', 'Error')}}</td> + <td>{{Spec2('ESDraft')}}</td> + <td></td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性" style="margin-bottom: 20px; line-height: 30px; font-size: 2.14285714285714rem;">浏览器兼容性</h2> + +<div>{{Compat("javascript.builtins.Error")}}</div> + +<h2 id="See_also" name="See_also" style="margin-bottom: 20px; line-height: 30px; font-size: 2.14285714285714rem;">相关链接</h2> + +<ul> + <li>{{jsxref("Error.prototype")}}</li> + <li>{{jsxref("Statements/throw", "throw")}}</li> + <li>{{jsxref("Statements/try...catch", "try...catch")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/global_objects/error/linenumber/index.html b/files/zh-cn/web/javascript/reference/global_objects/error/linenumber/index.html new file mode 100644 index 0000000000..a7b1ecd375 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/error/linenumber/index.html @@ -0,0 +1,54 @@ +--- +title: Error.prototype.lineNumber +slug: Web/JavaScript/Reference/Global_Objects/Error/lineNumber +tags: + - JavaScript + - 属性 + - 错误 +translation_of: Web/JavaScript/Reference/Global_Objects/Error/lineNumber +--- +<div>{{JSRef}} {{non-standard_header}}</div> + +<p><code><strong>lineNumber</strong></code> 属性的值为抛出错误的代码在其源文件中所在的行号。</p> + +<h2 id="示例">示例</h2> + +<h3 id="使用_lineNumber">使用 <code>lineNumber</code></h3> + +<pre class="brush: js">var e = new Error('Could not parse input'); +throw e; +console.log(e.lineNumber) // 2 +</pre> + +<h3 id="监听_error_事件的示例">监听 <code>error</code> 事件的示例</h3> + +<pre class="brush: js">window.addEventListener('error', function(e) { + console.log(e.lineNumber); // 5 +}); +var e = new Error('Could not parse input'); +throw e; +</pre> + +<p>这不是一个标准化的属性,缺乏广泛的支持。参见下面的浏览器兼容性表格。</p> + +<h2 id="规范">规范</h2> + +<p>非标准化属性。不属于任何规范。</p> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<div> +<div> + + +<p>{{Compat("javascript.builtins.Error.lineNumber")}}</p> +</div> +</div> + +<h2 id="相关内容">相关内容</h2> + +<ul> + <li>{{jsxref("Error.prototype.stack")}} {{non-standard_inline}}</li> + <li>{{jsxref("Error.prototype.columnNumber")}} {{non-standard_inline}}</li> + <li>{{jsxref("Error.prototype.fileName")}} {{non-standard_inline}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/global_objects/error/message/index.html b/files/zh-cn/web/javascript/reference/global_objects/error/message/index.html new file mode 100644 index 0000000000..92ba4e16c1 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/error/message/index.html @@ -0,0 +1,110 @@ +--- +title: Error.prototype.message +slug: Web/JavaScript/Reference/Global_Objects/Error/message +translation_of: Web/JavaScript/Reference/Global_Objects/Error/message +--- +<div>{{JSRef("Global_Objects", "Error", "EvalError,InternalError,RangeError,ReferenceError,SyntaxError,TypeError,URIError")}}</div> + +<h2 id="Summary" name="Summary">概述</h2> + +<p><code><strong>message</strong></code> 属性是有关错误信息,人类易读的(human-readable)描述。</p> + +<h2 id="Description" name="Description">描述</h2> + +<p>如果该属性已经被设置,则该属性包含了错误的一个简短描述。<a href="/en-US/docs/Mozilla/Projects/SpiderMonkey">SpiderMonkey</a> 大量应用 <code>message</code> 属性在异常方面。 <code>message</code> 属性结合 {{jsxref("Error.prototype.name", "name")}} 属性一起被 {{jsxref("Error.prototype.toString()")}} 方法用来创建错误的字符串形式。</p> + +<p>默认情况下,<code>message</code> 属性是一个空字符串,但是可以通过指定一段信息作为 {{jsxref("Error", "Error constructor")}} 的第一个参数创建一个实例来改变该属性值。</p> + +<h2 id="Examples" name="Examples">示例</h2> + +<h3 id="Example:_Throwing_a_custom_error" name="Example:_Throwing_a_custom_error">例子:抛出一个自定义错误</h3> + +<pre class="brush: js">var e = new Error("Could not parse input"); // e.message is "Could not parse input" +throw e; +</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.</td> + </tr> + <tr> + <td>{{SpecName('ES5.1', '#sec-15.11.4.3', 'Error.prototype.message')}}</td> + <td>{{Spec2('ES5.1')}}</td> + <td> </td> + </tr> + <tr> + <td>{{SpecName('ES6', '#sec-error.prototype.message', 'Error.prototype.message')}}</td> + <td>{{Spec2('ES6')}}</td> + <td> </td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<p>{{ CompatibilityTable() }}</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>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</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>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="See_also" name="See_also">相关链接</h2> + +<ul> + <li>{{jsxref("Error.prototype.name")}}</li> + <li>{{jsxref("Error.prototype.toString()")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/global_objects/error/name/index.html b/files/zh-cn/web/javascript/reference/global_objects/error/name/index.html new file mode 100644 index 0000000000..85eba29c8a --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/error/name/index.html @@ -0,0 +1,110 @@ +--- +title: Error.prototype.name +slug: Web/JavaScript/Reference/Global_Objects/Error/name +translation_of: Web/JavaScript/Reference/Global_Objects/Error/name +--- +<p>{{JSRef("Global_Objects", "Error", "EvalError,InternalError,RangeError,ReferenceError,SyntaxError,TypeError,URIError")}}</p> + +<h2 id="Summary" name="Summary">概述</h2> + +<p><code style="font-style: normal; line-height: 19.0909080505371px;"><strong>name</strong></code><span style="line-height: 19.0909080505371px;"> </span>属性表示error类型的名称.初始值为"Error".</p> + +<h2 id="Description" name="Description">描述</h2> + +<p>默认情况下,<span style="line-height: 19.0909080505371px;">{{jsxref("Error")}}</span>对象的<code>name</code>属性值为"Error".<code>name属性和</code><span style="line-height: 19.0909080505371px;">{{jsxref("Error.prototype.message", "message")}}</span>属性一起,通过调用<span style="line-height: 19.0909080505371px;">{{jsxref("Error.prototype.toString()")}}</span>方法,会作为最后异常信息的字符串表示.</p> + +<h2 id="Examples" name="Examples">示例</h2> + +<h3 id="Example:_Throwing_a_custom_error" name="Example:_Throwing_a_custom_error">例子: 抛出一个自定义错误</h3> + +<pre class="brush:js">var e = new Error("Malformed input"); // e.name默认是"Error" + +e.name = "ParseError"; // 修改之后,e.toString()会成为下面这样的字符串 +throw e; // "ParseError: Malformed input" +</pre> + +<h2 id="规范" style="margin-bottom: 20px; line-height: 30px; font-size: 2.14285714285714rem;">规范</h2> + +<table class="standard-table" style="line-height: 19.0909080505371px;"> + <tbody> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + <tr> + <td>ECMAScript 1st Edition.</td> + <td>Standard</td> + <td>Initial definition.</td> + </tr> + <tr> + <td>{{SpecName('ES5.1', '#sec-15.11.4.2', 'Error.prototype.name')}}</td> + <td>{{Spec2('ES5.1')}}</td> + <td> </td> + </tr> + <tr> + <td>{{SpecName('ES6', '#sec-error.prototype.name', 'Error.prototype.name')}}</td> + <td>{{Spec2('ES6')}}</td> + <td> </td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性" style="margin-bottom: 20px; line-height: 30px; font-size: 2.14285714285714rem;">浏览器兼容性</h2> + +<div style="line-height: 19.0909080505371px;">{{CompatibilityTable}}</div> + +<div id="compat-desktop" style="line-height: 19.0909080505371px;"> +<table class="compat-table" style="border-color: transparent;"> + <tbody> + <tr> + <th style="line-height: 16px;">Feature</th> + <th style="line-height: 16px;">Chrome</th> + <th style="line-height: 16px;">Firefox (Gecko)</th> + <th style="line-height: 16px;">Internet Explorer</th> + <th style="line-height: 16px;">Opera</th> + <th style="line-height: 16px;">Safari</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile" style="line-height: 19.0909080505371px;"> +<table class="compat-table" style="border-color: transparent;"> + <tbody> + <tr> + <th style="line-height: 16px;">Feature</th> + <th style="line-height: 16px;">Android</th> + <th style="line-height: 16px;">Chrome for Android</th> + <th style="line-height: 16px;">Firefox Mobile (Gecko)</th> + <th style="line-height: 16px;">IE Mobile</th> + <th style="line-height: 16px;">Opera Mobile</th> + <th style="line-height: 16px;">Safari Mobile</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="See_also" name="See_also" style="margin-bottom: 20px; line-height: 30px; font-size: 2.14285714285714rem;">相关链接</h2> + +<ul style="line-height: 19.0909080505371px;"> + <li>{{jsxref("Error.prototype.message")}}</li> + <li>{{jsxref("Error.prototype.toString()")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/global_objects/error/prototype/index.html b/files/zh-cn/web/javascript/reference/global_objects/error/prototype/index.html new file mode 100644 index 0000000000..aea7d132d2 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/error/prototype/index.html @@ -0,0 +1,161 @@ +--- +title: Error.prototype +slug: Web/JavaScript/Reference/Global_Objects/Error/prototype +tags: + - Error + - JavaScript + - Property + - 参考 + - 属性 +translation_of: Web/JavaScript/Reference/Global_Objects/Error +--- +<div> +<p>{{JSRef}}</p> + +<p><code><strong>Error.prototype</strong></code> 属性代表 {{jsxref("Error")}} 的构造器。</p> + +<p>{{js_property_attributes(0, 0, 0)}}</p> +</div> + +<h2 id="Description" name="Description">描述</h2> + +<p>所有 {{jsxref("Global_Objects/Error", "Error")}} 与 {{jsxref("Global_Objects/Error", "非标准Error", "#Error_types", 1)}} 的实例都继承自 <code>Error.prototype。同所有构造器函数一样,你可以在构造器的 </code>prototype 上添加属性或者方法,使其在所有该构造器的实例上生效。</p> + +<h2 id="Properties" name="Properties">属性</h2> + +<h3 id="Standard_properties" name="Standard_properties">标准属性</h3> + +<dl> + <dt><code>Error.prototype.constructor</code></dt> + <dd>实例原型的构造函数。</dd> + <dt>{{jsxref("Error.prototype.message")}}</dt> + <dd>错误信息。</dd> + <dt>{{jsxref("Error.prototype.name")}}</dt> + <dd>错误名。</dd> +</dl> + +<h3 id="Vendor-specific_extensions" name="Vendor-specific_extensions">厂商特定扩展属性</h3> + +<div>{{non-standard_header}}</div> + +<h4 id="Microsoft" name="Microsoft">Microsoft</h4> + +<dl> + <dt>{{jsxref("Error.prototype.description")}}</dt> + <dd>错误描述,与 {{jsxref("Error.prototype.message", "message")}} 相似。</dd> + <dt>{{jsxref("Error.prototype.number")}}</dt> + <dd>错误码。</dd> +</dl> + +<h4 id="Mozilla" name="Mozilla">Mozilla</h4> + +<dl> + <dt>{{jsxref("Error.prototype.fileName")}}</dt> + <dd>产生该错误的文件名。</dd> + <dt>{{jsxref("Error.prototype.lineNumber")}}</dt> + <dd>产生该错误的行号。</dd> + <dt>{{jsxref("Error.prototype.columnNumber")}}</dt> + <dd>产生该错误的列号。</dd> + <dt>{{jsxref("Error.prototype.stack")}}</dt> + <dd>错误堆栈。</dd> +</dl> + +<h2 id="Methods" name="Methods">方法</h2> + +<dl> + <dt>{{jsxref("Error.prototype.toSource()")}} {{non-standard_inline}}</dt> + <dd>返回一个包含特定 {{jsxref("Error")}} 对象的源代码字符串,你可以用该值新建一个新的对象,重写自 {{jsxref("Object.prototype.toSource()")}} 方法。</dd> + <dt>{{jsxref("Error.prototype.toString()")}}</dt> + <dd>返回一个表示该对象的字符串,重写自 {{jsxref("Object.prototype.toString()")}} 方法。</dd> +</dl> + +<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.1.</td> + </tr> + <tr> + <td>{{SpecName('ES5.1', '#sec-15.11.3.1', 'Error')}}</td> + <td>{{Spec2('ES5.1')}}</td> + <td> </td> + </tr> + <tr> + <td>{{SpecName('ES6', '#sec-error.prototype', 'Error')}}</td> + <td>{{Spec2('ES6')}}</td> + <td> </td> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-error.prototype', 'Error')}}</td> + <td>{{Spec2('ESDraft')}}</td> + <td> </td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<div>{{CompatibilityTable}}</div> + +<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>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</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>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="See_also" name="See_also">参见</h2> + +<ul> + <li>{{jsxref("Error")}}</li> + <li>{{jsxref("Object.prototype")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/global_objects/error/stack/index.html b/files/zh-cn/web/javascript/reference/global_objects/error/stack/index.html new file mode 100644 index 0000000000..b828e71662 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/error/stack/index.html @@ -0,0 +1,126 @@ +--- +title: Error.prototype.stack +slug: Web/JavaScript/Reference/Global_Objects/Error/Stack +tags: + - JavaScript + - 原型 + - 参考 + - 属性 + - 错误 +translation_of: Web/JavaScript/Reference/Global_Objects/Error/Stack +--- +<div>{{JSRef}} {{non-standard_header}}</div> + +<p>{{jsxref("Error")}}对象作为一个非标准的栈属性提供了一种函数追踪方式。无论这个函数被被调用,处于什么模式,来自于哪一行或者哪个文件,有着什么样的参数。这个栈产生于最近一次调用最早的那次调用,返回原始的全局作用域调用</p> + +<h2 id="描述">描述</h2> + +<p>每个步骤都会被分为单独的一行以这个函数的名字作为开始(如果不是一个来自于全局作用域的调用),然后通过一个@符号标记一个文件的位置(尤其是当一个函数构造错误并且作为错误被抛出,并且如果能定位到这个文件的位置,那么会使用冒号显示行号。(提示){{jsxref("Error")}}对象在错误跑出时同样能处理并渲染出文件名,行号和列号属性(但是仅仅限于错误。而不是追踪他的路径)</p> + +<p>注意这是Firefox定义的格式,并没有标准的定义。但是Safari 6+ 和 Opera 12-定义了一种非常相似的格式。其他使用JavaScript V8引擎的浏览器(例如Chrome, Opera 15+,安卓浏览器)和IE 10+,定义了一种不同的格式(可参见 <a class="external" href="http://msdn.microsoft.com/en-us/library/windows/apps/hh699850.aspx">error.stack</a> 文档)</p> + +<p>堆栈中的参数值: Firefox 14版本之前是 ({{bug("744842")}})函数名会随着参数值会在添加@符号之前被立即转换成用圆括号包裹的string类型。然而对象或者数组等其他类型似乎会被转换成<code>"[object Object]"</code>并且这种格式不能回退到之前实际上的对象,而纯值会被渲染(或许这种在Firefox14中仍有这种可能,使用<code>arguments.callee.caller.arguments</code>更加简单。因为函数名可以使用<code>arguments.callee.caller.name</code>渲染)。<code>"undefined"</code>被显示为<code>"(void 0)"</code>不过要注意的是如果是字符串类型的参数会直接以类似<code>"@"</code>, <code>"("</code>, <code>")"</code>格式通过编译(或者是包含在文件名中)。你不能简单的依赖这些将它分成多个组件,但是,对于Firefox14及以后的版本来说,这些都不是问题</p> + +<p>不同的浏览器会在不同时期设置这个值。例如,Firefox在创建{{jsxref("Error")}}对象时设置它,然而PhantomJS是在当且仅当它抛出 {{jsxref("Error")}}时, 并且<a href="http://msdn.microsoft.com/en-us/library/windows/apps/hh699850.aspx">MSDN docs</a> 似乎也实现了PhantomJS的方式。</p> + +<h2 id="示例">示例</h2> + +<p>下面这段html代码展示了<code>stack</code> 属性的使用方法</p> + +<pre class="brush: html"><!DOCTYPE HTML> +<meta charset="UTF-8"> +<title>Stack Trace Example</title> +<body> +<script> +function trace() { + try { + throw new Error('myError'); + } + catch(e) { + alert(e.stack); + } +} +function b() { + trace(); +} +function a() { + b(3, 4, '\n\n', undefined, {}); +} +a('first call, firstarg'); +</script> + +</pre> + +<p>假设上面这段代码被保存在Windows系统下的 <code>C:\example.html</code>在处理过程中抛出如下所示的错误信息</p> + +<p>Firefox 30及以上版本的浏览器会包含以列号为开始 ({{bug("762556")}}):</p> + +<pre><samp>trace@file:///C:/example.html:9:17 +b@file:///C:/example.html:16:13 +a@file:///C:/example.html:19:13 +@file:///C:/example.html:21:9</samp></pre> + +<p>Firefox 14 to Firefox 29:</p> + +<pre><samp>trace@file:///C:/example.html:9 +b@file:///C:/example.html:16 +a@file:///C:/example.html:19 +@file:///C:/example.html:21</samp></pre> + +<p>Firefox 13及更早版本的浏览器会抛出如下信息:</p> + +<pre><samp>Error("myError")@:0 +trace()@file:///C:/example.html:9 +b(3,4,"\n\n",(void 0),[object Object])@file:///C:/example.html:16 +a("first call, firstarg")@file:///C:/example.html:19 +@file:///C:/example.html:21</samp></pre> + +<h3 id="Stack_of_eval'ed_code">Stack of eval'ed code</h3> + +<p>Firefox30以{{geckoRelease("30")}}格式开头,<code>Function()</code> 和 <code>eval()</code> 调用产生的错误代码堆栈,现在在调用内部通过行号和列号以更加详细的格式向我们展示出来。函数调用显示为<code>"> Function"</code> 而 eval调用则是 <code>"> eval"</code>这样。下面来看这个{{bug("332176")}}.</p> + +<pre class="brush: js">try { + new Function('throw new Error()')(); +} catch (e) { + console.log(e.stack); +} + +// anonymous@file:///C:/example.html line 7 > Function:1:1 +// @file:///C:/example.html:7:6 + + +try { + eval("eval('FAIL')"); +} catch (x) { + console.log(x.stack); +} + +// @file:///C:/example.html line 7 > eval line 1 > eval:1:1 +// @file:///C:/example.html line 7 > eval:1:1 +// @file:///C:/example.html:7:6 +</pre> + +<p>你也可以使用<code>//# sourceURL</code> 命名eval源的指令。 也可以查看在 <a href="/en-US/docs/Tools/Debugger">Debugger</a>文档中的<a href="/en-US/docs/Tools/Debugger/How_to/Debug_eval_sources" style="">Debug eval 源</a>和<a href="http://fitzgeraldnick.com/weblog/59/">blog post博客 。</a></p> + +<h2 id="规范">规范</h2> + +<p>不属于任何规范,没有标准规范。</p> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<div> +<div> +<div class="hidden">此页面上的兼容性表由数据结构化生成。 如果您想为此做出贡献,请查看<a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> 并向我们发起拉取请求。</div> + +<p>{{Compat("javascript.builtins.Error.stack")}}</p> +</div> +</div> + +<h2 id="另请参阅">另请参阅</h2> + +<ul> + <li><a href="/en-US/docs/Components.stack">Components.stack</a></li> + <li>外部项目: <a class="link-https" href="https://github.com/csnover/TraceKit/">TraceKit</a> and <a class="link-https" href="https://github.com/eriwen/javascript-stacktrace">javascript-stacktrace</a></li> + <li>MSDN: <a class="external" href="http://msdn.microsoft.com/en-us/library/windows/apps/hh699850.aspx" title="http://msdn.microsoft.com/en-us/library/windows/apps/hh699850.aspx">error.stack</a> docs</li> + <li><a href="https://github.com/v8/v8/wiki/Stack%20Trace%20API">Overview of the V8 JavaScript stack trace API</a></li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/global_objects/error/tosource/index.html b/files/zh-cn/web/javascript/reference/global_objects/error/tosource/index.html new file mode 100644 index 0000000000..89273b1f32 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/error/tosource/index.html @@ -0,0 +1,53 @@ +--- +title: Error.prototype.toSource() +slug: Web/JavaScript/Reference/Global_Objects/Error/toSource +translation_of: Web/JavaScript/Reference/Global_Objects/Error/toSource +--- +<div>{{JSRef}} {{non-standard_header}}</div> + +<p><code><strong>toSource()</strong></code> 方法返回可以计算出到相同错误的代码。</p> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox"><var>e</var>.toSource()</pre> + +<h3 id="返回值">返回值</h3> + +<p>一个包含错误源代码的字符串</p> + +<h2 id="描述">描述</h2> + +<p>调用一个{{jsxref("Error")}}实例的<code>toSource</code>方法(包括 <em><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error#Error_types">NativeErrors</a></em>)将返回包含错误源代码的字符串。 这个字符串可以被评估创建一个(大约)相等的对象。当然,包含源字符串遵循的结构{{ jsxref("Error")}} 构造函数。例如:</p> + +<pre class="brush: js">(new<em>name</em>(<em>message</em> ,<em>fileName</em>,<em>lineNumber</em>)) +</pre> + +<p>这些属性对应于错误实例的相应属性。</p> + +<div class="note"> +<p><strong>提示:</strong> 需要注意到,在创建该字符串时,<code>toSource</code>方法所使用的属性是可变的,并且可能无法准确地反映用于创建错误实例的函数或实际发生错误的文件名或行号。</p> +</div> + +<h2 id="规范">规范</h2> + +<p>不属于任何规范. 应用于 in JavaScript 1.3.</p> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<div> +<div> +<div class="hidden">这个页面上的兼容性表是由结构化数据生成的。如果您想对数据作出贡献,请查看https://github.com/mdn/browser-compat-data并发送一个pull请求。</div> + +<p>{{Compat("javascript.builtins.Error.toSource")}}</p> +</div> +</div> + +<h2 id="相关链接">相关链接</h2> + +<ul> + <li>{{jsxref("Error.prototype.fileName")}}</li> + <li>{{jsxref("Error.prototype.lineNumber")}}</li> + <li>{{jsxref("Error.prototype.message")}}</li> + <li>{{jsxref("Error.prototype.name")}}</li> + <li>{{jsxref("Object.prototype.toSource()")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/global_objects/error/tostring/index.html b/files/zh-cn/web/javascript/reference/global_objects/error/tostring/index.html new file mode 100644 index 0000000000..a32f0919d3 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/error/tostring/index.html @@ -0,0 +1,129 @@ +--- +title: Error.prototype.toString() +slug: Web/JavaScript/Reference/Global_Objects/Error/toString +translation_of: Web/JavaScript/Reference/Global_Objects/Error/toString +--- +<div> + {{JSRef("Global_Objects", "Error", "EvalError,InternalError,RangeError,ReferenceError,SyntaxError,TypeError,URIError")}}</div> +<h2 id="Summary" name="Summary">概述</h2> +<p><code><strong>toString()</strong></code> 方法返回一个指定的错误对象(Error object)的字符串表示。</p> +<h2 id="Syntax" name="Syntax">语法</h2> +<pre class="syntaxbox"><code><em>e</em>.toString();</code></pre> +<h2 id="Description" name="Description">描述</h2> +<p>{{jsxref("Error")}} 对象覆盖了 {{jsxref("Object.prototype.toString()")}} 方法。该方法实现如下:(假定 <code>Object</code> 和 <code>String</code> 没有被更改):</p> +<pre class="brush: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; +}; +</pre> +<h2 id="Example" name="Example">示例</h2> +<pre class="brush: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" +</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>{{SpecName('ES5.1', '#sec-15.11.4.4', 'Error.prototype.toString')}}</td> + <td>{{Spec2('ES5.1')}}</td> + <td> </td> + </tr> + <tr> + <td>{{SpecName('ES6', '#sec-error.prototype.tostring', 'Error.prototype.toString')}}</td> + <td>{{Spec2('ES6')}}</td> + <td> </td> + </tr> + </tbody> +</table> +<h2 id="浏览器兼容性">浏览器兼容性</h2> +<p>{{ CompatibilityTable() }}</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>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</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>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + </tr> + </tbody> + </table> +</div> +<h2 id="See_also" name="See_also">相关链接</h2> +<ul> + <li>{{jsxref("Error.prototype.toSource()")}}</li> +</ul> |