158 lines
13 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>
<p><strong>RangeError</strong>对象标明一个错误,当一个值不在其所允许的范围或者集合中。</p>
<h2 id="语法">语法</h2>
<pre><code class="language-javascript"><code>new RangeError([<var>message</var>[, <var>fileName</var>[, <var>lineNumber</var>]]])</code></code></pre>
<h3 id="参数">参数</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="描述">描述</h2>
<p>试图传递一个number参数给一个范围内不包含该number的函数时则会引发<strong>RangeError</strong>。当传递一个不合法的length值作为<a href="Reference/Array" title="REDIRECT Array"><code>Array</code></a> 构造器的参数创建数组,或者传递错误值到数值计算方法(<a href="Reference/Global_Objects/Number/toExponential" title="toExponential() 方法以指数表示法返回该数值字符串表示形式。"><code>Number.toExponential()</code></a><a href="Reference/Global_Objects/Number/toFixed" title="toFixed() 方法使用定点表示法来格式化一个数。"><code>Number.toFixed()</code></a> <a href="Reference/Global_Objects/Number/toPrecision" title="toPrecision() 方法以指定的精度返回该数值对象的字符串表示。"><code>Number.toPrecision()</code></a>),会出现<strong>RangeError</strong>。.</p>
<h2 id="属性">属性</h2>
<dl>
<dt><a href="Reference/Global_Objects/RangeError/prototype" title="所有  RangeError 的实例都继承自 RangeError.prototype ,所以你可以使用这个属性来为所有的实例添加属性或方法。"><code>RangeError.prototype</code></a></dt>
<dd><code>允许在<strong>RangeError</strong>对象上附加属性。</code></dd>
</dl>
<h2 id="方法">方法</h2>
<p><code>RangeError</code>全局对象没有自带方法,但它通过可以原型链继承一些方法。</p>
<h2 id="RangeError实例"><code>RangeError</code>实例</h2>
<h3 id="属性_2">属性</h3>
<div><dl>
<dt><code>RangeError.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>RangeError.prototype.message</code></a></dt>
<dd>Error message. Although ECMA-262 specifies that <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/RangeError" title="The RangeError object indicates an error when a value is not in the set or range of allowed values."><code>RangeError</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>RangeError.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>RangeError.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>RangeError.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>RangeError.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>RangeError.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="方法_2">方法</h3>
<div><p>Although the <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/RangeError" title="The RangeError object indicates an error when a value is not in the set or range of allowed values."><code>RangeError</code></a> prototype object does not contain any methods of its own, <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/RangeError" title="The RangeError object indicates an error when a value is not in the set or range of allowed values."><code>RangeError</code></a> instances do inherit some methods through the prototype chain.</p></div>
<h2 id="例子">例子</h2>
<h3 id="使用RangeError">使用<code>RangeError</code></h3>
<pre><code class="language-javascript">var check = function(num) {
if (num &lt; MIN || num &gt; MAX) {
throw new RangeError('Parameter must be between ' + MIN + ' and ' + MAX);
}
};
try {
check(500);
}
catch (e) {
if (e instanceof RangeError) {
// 处理越界错误
}
}
</code></pre>
<h2 id="规范">规范</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><a class="external" href="https://www.ecma-international.org/publications/files/ECMA-ST-ARCH/ECMA-262,%203rd%20edition,%20December%201999.pdf" hreflang="en" lang="en" rel="noopener" title="ECMAScript 3rd Edition (ECMA-262)">ECMAScript 3rd Edition (ECMA-262)</a></td>
<td><span class="spec-Standard">Standard</span></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.2" hreflang="en" lang="en" rel="noopener">ECMAScript 5.1 (ECMA-262)<br/><small lang="zh-CN">RangeError</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-rangeerror" hreflang="en" lang="en" rel="noopener">ECMAScript 2015 (6th Edition, ECMA-262)<br/><small lang="zh-CN">RangeError</small></a></td>
<td><span class="spec-Standard">Standard</span></td>
<td> </td>
</tr>
<tr>
<td><a class="external" href="https://tc39.github.io/ecma262/#sec-native-error-types-used-in-this-standard-rangeerror" hreflang="en" lang="en" rel="noopener">ECMAScript Latest Draft (ECMA-262)<br/><small lang="zh-CN">RangeError</small></a></td>
<td><span class="spec-Draft">Draft</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="相关连接">相关连接</h2>
<ul>
<li><a href="Reference/Global_Objects/Error" title="通过Error的构造器可以创建一个错误对象。当运行时错误产生时Error的实例对象会被抛出。Error对象也可用于用户自定义的异常的基础对象。下面列出了各种内建的标准错误类型。"><code>Error</code></a></li>
<li><a href="Reference/Global_Objects/RangeError/prototype" title="所有  RangeError 的实例都继承自 RangeError.prototype ,所以你可以使用这个属性来为所有的实例添加属性或方法。"><code>RangeError.prototype</code></a></li>
<li><a href="Reference/Array" title="REDIRECT Array"><code>Array</code></a></li>
<li><a href="Reference/Global_Objects/Number/toExponential" title="toExponential() 方法以指数表示法返回该数值字符串表示形式。"><code>Number.toExponential()</code></a></li>
<li><a href="Reference/Global_Objects/Number/toFixed" title="toFixed() 方法使用定点表示法来格式化一个数。"><code>Number.toFixed()</code></a></li>
<li><a href="Reference/Global_Objects/Number/toPrecision" title="toPrecision() 方法以指定的精度返回该数值对象的字符串表示。"><code>Number.toPrecision()</code></a></li>
</ul>
</article>