首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

RangeError

RangeError对象标明一个错误,当一个值不在其所允许的范围或者集合中。

语法

代码语言:javascript
复制
new RangeError([message[, fileName[, lineNumber]]])

参数

message可选,可读的错误描述fileName可选,包含造成异常代码的文件名lineNumber可选,造成异常的代码所在的行数

描述

试图传递一个number参数给一个范围内不包含该number的函数时则会引发RangeError。当传递一个不合法的length值作为Array构造器的参数创建数组,或者传递错误值到数值计算方法(Number.toExponential()Number.toFixed()Number.toPrecision()),会出现RangeError

属性

RangeError.prototype允许在RangeError对象上附加属性。

方法

RangeError全局对象没有自带方法,但它通过可以原型链继承一些方法。

RangeError实例

属性

RangeError.prototype.constructorSpecifies the function that created an instance's prototype.

RangeError.prototype.messageError message. Although ECMA-262 specifies thatRangeErrorshould provide its ownmessageproperty, inSpiderMonkey, it inheritsError.prototype.message.

RangeError.prototype.nameError name. Inherited fromError.

RangeError.prototype.fileNamePath to file that raised this error. Inherited fromError.

RangeError.prototype.lineNumberLine number in file that raised this error. Inherited fromError.

RangeError.prototype.columnNumberColumn number in line that raised this error. Inherited fromError.

RangeError.prototype.stackStack trace. Inherited fromError.

方法

尽管RangeError原型对象本身不包含任何方法,但RangeError实例通过原型链继承了一些方法。

示例

使用RangeError

代码语言:javascript
复制
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) {
    // Handle range error
  }
}

规范

Specification

Status

Comment

ECMAScript 3rd Edition (ECMA-262)

Standard

Initial definition.

ECMAScript 5.1 (ECMA-262)The definition of 'RangeError' in that specification.

Standard

?

ECMAScript 2015 (6th Edition, ECMA-262)The definition of 'RangeError' in that specification.

Standard

?

ECMAScript Latest Draft (ECMA-262)The definition of 'RangeError' in that specification.

Living Standard

?

浏览器兼容性

Feature

Chrome

Edge

Firefox

Internet Explorer

Opera

Safari

Basic Support

(Yes)

(Yes)

(Yes)

(Yes)

(Yes)

(Yes)

Feature

Android

Chrome for Android

Edge mobile

Firefox for Android

IE mobile

Opera Android

iOS Safari

Basic Support

(Yes)

(Yes)

(Yes)

(Yes)

(Yes)

(Yes)

(Yes)

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com