此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。
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 2015年7月.
Error.prototype 的name 数据属性是所有Error 实例所共享的。它表示当前错误类型的名称。对于Error.prototype.name,其初始值为"Error"。像TypeError 和SyntaxError 这样的子类会提供它们自己的name 属性。
In this article
值
字符串。对于Error.prototype.name,其初始值为"Error"。
Error.prototype.name 的属性特性 | |
|---|---|
| 可写 | 是 |
| 可枚举 | 否 |
| 可配置 | 是 |
描述
默认情况下,为Error 实例提供的名称为“Error”。Error.prototype.toString() 方法会同时使用name 和message 属性来创建错误信息的字符串表示。
示例
>抛出一个自定义错误
js
const e = new Error("Malformed input"); // e.name 为“Error”e.name = "ParseError";throw e;// e.toString() 会返回“ParseError: Malformed input”规范
| Specification |
|---|
| ECMAScript® 2026 Language Specification> # sec-error.prototype.name> |