错误:message

Error 实例的 message 数据属性是人类可读的错误描述。

¥The message data property of an Error instance is a human-readable description of the error.

¥Value

与作为第一个参数传递给 Error() 构造函数的值相对应的字符串。

¥A string corresponding to the value passed to the Error() constructor as the first argument.

Property attributes of 错误:message
Writable yes
Enumerable no
Configurable yes

描述

¥Description

此属性包含错误的简要描述(如果可用或已设置)。Error.prototype.toString() 方法使用 message 属性与 name 属性组合来创建错误的字符串表示形式。

¥This property contains a brief description of the error if one is available or has been set. The message property combined with the name property is used by the Error.prototype.toString() method to create a string representation of the Error.

默认情况下,message 属性是空字符串,但可以通过将消息指定为 Error 构造函数的第一个参数来覆盖实例的此行为。

¥By default, the message property is an empty string, but this behavior can be overridden for an instance by specifying a message as the first argument to the Error constructor.

示例

¥Examples

抛出自定义错误

¥Throwing a custom error

js
const e = new Error("Could not parse input");
// e.message is 'Could not parse input'
throw e;

规范

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

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看