Error.prototype.name

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

概述

name 属性表示 error 类型的名称。初始值为"Error".

描述

默认情况下,Error对象的name属性值为"Error".name 属性和message属性一起,通过调用Error.prototype.toString()方法,会作为最后异常信息的字符串表示。

示例

示例:抛出一个自定义错误

js
var e = new Error("Malformed input"); // e.name 默认是"Error"

e.name = "ParseError"; // 修改之后,e.toString() 会成为下面这样的字符串
throw e; // "ParseError: Malformed input"

规范

Specification
ECMAScript Language Specification
# sec-error.prototype.name

浏览器兼容性

BCD tables only load in the browser

参见