2019-04-21 11:50:48 +08:00

160 lines
12 KiB
HTML
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<article id="wikiArticle">
<div></div>
<h2 id="Summary" name="Summary">概述</h2>
<p><code><strong>ReferenceError引用错误</strong></code> 对象代表当一个不存在的变量被引用时发生的错误。</p>
<h2 id="Syntax" name="Syntax">语法</h2>
<pre><code class="language-javascript"><code>new ReferenceError([<var>message</var>[, <var>fileName</var>[, <var>lineNumber</var>]]])</code></code></pre>
<h3 id="Parameters" name="Parameters">参数</h3>
<dl>
<dt><code>message</code></dt>
<dd>可选。描述可读的错误信息</dd>
<dt><code>fileName</code> <span class="icon-only-inline" title="This API has not been standardized."><i class="icon-warning-sign"> </i></span></dt>
<dd>可选。包含引起异常代码的文件名</dd>
<dt><code>lineNumber</code> <span class="icon-only-inline" title="This API has not been standardized."><i class="icon-warning-sign"> </i></span></dt>
<dd>可选。引起异常的代码行号</dd>
</dl>
<h2 id="Description" name="Description">描述</h2>
<p>当你尝试引用一个未被定义的变量时,将会抛出一个 <code>ReferenceError</code></p>
<h2 id="Properties" name="Properties">属性</h2>
<dl>
<dt><a href="Reference/Global_Objects/ReferenceError/prototype" title="ReferenceError.prototype 表示 ReferenceError 的原型构造器。"><code>ReferenceError.prototype</code></a></dt>
<dd>Allows the addition of properties to an <code>ReferenceError</code> object.</dd>
</dl>
<h2 id="Methods" name="Methods">方法</h2>
<p>全局的 <code>ReferenceError</code> 本身并不包含有方法,但是他可以从原型链上继承一些方法</p>
<h2 id="ReferenceError_instances" name="ReferenceError_instances"><code>ReferenceError</code> 实例</h2>
<h3 id="Properties_of_ReferenceError_instances" name="Properties_of_ReferenceError_instances">属性</h3>
<div><dl>
<dt><code>ReferenceError.prototype.constructor</code></dt>
<dd>Specifies the function that created an instance's prototype.</dd>
<dt><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/message" title="The message property is a human-readable description of the error."><code>ReferenceError.prototype.message</code></a></dt>
<dd>Error message. Although ECMA-262 specifies that <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/ReferenceError" title="The ReferenceError object represents an error when a non-existent variable is referenced."><code>ReferenceError</code></a> should provide its own <code>message</code> property, in <a href="/en-US/docs/Mozilla/Projects/SpiderMonkey">SpiderMonkey</a>, it inherits <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/message" title="The message property is a human-readable description of the error."><code>Error.prototype.message</code></a>.</dd>
<dt><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/name" title='The name property represents a name for the type of error. The initial value is "Error".'><code>ReferenceError.prototype.name</code></a></dt>
<dd>Error name. Inherited from <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" title="The Error constructor creates an error object. Instances of Error objects are thrown when runtime errors occur. The Error object can also be used as a base object for user-defined exceptions. See below for standard built-in error types."><code>Error</code></a>.</dd>
<dt><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/fileName" title="The fileName property contains the path to the file that raised this error."><code>ReferenceError.prototype.fileName</code></a></dt>
<dd>Path to file that raised this error. Inherited from <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" title="The Error constructor creates an error object. Instances of Error objects are thrown when runtime errors occur. The Error object can also be used as a base object for user-defined exceptions. See below for standard built-in error types."><code>Error</code></a>.</dd>
<dt><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/lineNumber" title="The lineNumber property contains the line number in the file that raised this error."><code>ReferenceError.prototype.lineNumber</code></a></dt>
<dd>Line number in file that raised this error. Inherited from <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" title="The Error constructor creates an error object. Instances of Error objects are thrown when runtime errors occur. The Error object can also be used as a base object for user-defined exceptions. See below for standard built-in error types."><code>Error</code></a>.</dd>
<dt><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/columnNumber" title="The columnNumber property contains the column number in the line of the file that raised this error."><code>ReferenceError.prototype.columnNumber</code></a></dt>
<dd>Column number in line that raised this error. Inherited from <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" title="The Error constructor creates an error object. Instances of Error objects are thrown when runtime errors occur. The Error object can also be used as a base object for user-defined exceptions. See below for standard built-in error types."><code>Error</code></a>.</dd>
<dt><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/stack" title="The non-standard stack property of Error objects offer a trace of which functions were called, in what order, from which line and file, and with what arguments. The stack string proceeds from the most recent calls to earlier ones, leading back to the original global scope call."><code>ReferenceError.prototype.stack</code></a></dt>
<dd>Stack trace. Inherited from <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" title="The Error constructor creates an error object. Instances of Error objects are thrown when runtime errors occur. The Error object can also be used as a base object for user-defined exceptions. See below for standard built-in error types."><code>Error</code></a>.</dd>
</dl></div>
<h3 id="Methods_of_ReferenceError_instances" name="Methods_of_ReferenceError_instances">方法</h3>
<div><p>Although the <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/ReferenceError" title="The ReferenceError object represents an error when a non-existent variable is referenced."><code>ReferenceError</code></a> prototype object does not contain any methods of its own, <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/ReferenceError" title="The ReferenceError object represents an error when a non-existent variable is referenced."><code>ReferenceError</code></a> instances do inherit some methods through the prototype chain.</p></div>
<h2 id="Examples" name="Examples">例子</h2>
<h3 id="Example:_Catch_an_ReferenceError" name="Example:_Catch_an_ReferenceError">例: 捕获一个 <code>ReferenceError</code></h3>
<pre><code class="language-javascript">try {
var a = undefinedVariable;
} catch (e) {
console.log(e instanceof ReferenceError); // true
console.log(e.message); // "undefinedVariable is not defined"
console.log(e.name); // "ReferenceError"
console.log(e.fileName); // "Scratchpad/1"
console.log(e.lineNumber); // 2
console.log(e.columnNumber); // 6
console.log(e.stack); // "@Scratchpad/2:2:7\n"
}
</code></pre>
<h3 id="Example:_Create_an_ReferenceError" name="Example:_Create_an_ReferenceError">例: 创建一个 <code>ReferenceError</code></h3>
<pre><code class="language-javascript">try {
throw new ReferenceError('Hello', 'someFile.js', 10);
} catch (e) {
console.log(e instanceof ReferenceError); // true
console.log(e.message); // "Hello"
console.log(e.name); // "ReferenceError"
console.log(e.fileName); // "someFile.js"
console.log(e.lineNumber); // 10
console.log(e.columnNumber); // 0
console.log(e.stack); // "@Scratchpad/2:2:9\n"
}
</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 3rd Edition.</td>
<td>Standard</td>
<td>Initial definition.</td>
</tr>
<tr>
<td><a class="external" href="https://www.ecma-international.org/ecma-262/5.1/#sec-15.11.6.3" hreflang="en" lang="en" rel="noopener">ECMAScript 5.1 (ECMA-262)<br/><small lang="zh-CN">ReferenceError</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-native-error-types-used-in-this-standard-referenceerror" hreflang="en" lang="en" rel="noopener">ECMAScript 2015 (6th Edition, ECMA-262)<br/><small lang="zh-CN">ReferenceError</small></a></td>
<td><span class="spec-Standard">Standard</span></td>
<td> </td>
</tr>
</tbody>
</table>
<h2 id="浏览器兼容性">浏览器兼容性</h2>
<div><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></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><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" title="通过Error的构造器可以创建一个错误对象。当运行时错误产生时Error的实例对象会被抛出。Error对象也可用于用户自定义的异常的基础对象。下面列出了各种内建的标准错误类型。"><code>Error</code></a></li>
<li><a href="Reference/Global_Objects/ReferenceError/prototype" title="ReferenceError.prototype 表示 ReferenceError 的原型构造器。"><code>ReferenceError.prototype</code></a></li>
</ul>
</article>