--- title: RangeError slug: Web/JavaScript/Reference/Global_Objects/RangeError translation_of: Web/JavaScript/Reference/Global_Objects/RangeError ---
RangeError对象标明一个错误,当一个值不在其所允许的范围或者集合中。
new RangeError([message[, fileName[, lineNumber]]])
message
fileName
{{non-standard_inline}}lineNumber
{{non-standard_inline}}试图传递一个number参数给一个范围内不包含该number的函数时则会引发RangeError。当传递一个不合法的length值作为{{jsxref("Array")}} 构造器的参数创建数组,或者传递错误值到数值计算方法({{jsxref("Number.toExponential()")}},{{jsxref("Number.toFixed()")}} ,{{jsxref("Number.toPrecision()")}}),会出现RangeError。.
允许在RangeError对象上附加属性。
RangeError
全局对象没有自带方法,但它通过可以原型链继承一些方法。
RangeError
实例RangeError
var check = function(num) { if (num < MIN || num > MAX) { throw new RangeError('Parameter must be between ' + MIN + ' and ' + MAX); } }; try { check(500); } catch (e) { if (e instanceof RangeError) { // 处理越界错误 } }
Specification | Status | Comment |
---|---|---|
{{SpecName('ES3')}} | {{Spec2('ES3')}} | Initial definition. |
{{SpecName('ES5.1', '#sec-15.11.6.2', 'RangeError')}} | {{Spec2('ES5.1')}} | |
{{SpecName('ES6', '#sec-native-error-types-used-in-this-standard-rangeerror', 'RangeError')}} | {{Spec2('ES6')}} | |
{{SpecName('ESDraft', '#sec-native-error-types-used-in-this-standard-rangeerror', 'RangeError')}} | {{Spec2('ESDraft')}} |
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} |
Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Basic support | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} |