error.message


error.message 属性是通过调用 new Error(message) 设置的错误的字符串描述。 传给构造函数的 message 也会出现在 Error 的堆栈跟踪的第一行,但是在 Error 对象创建后更改此属性可能不会更改堆栈跟踪的第一行(例如,当读取 error.stack 时 在此属性更改之前)。

const err = new Error('The message');
console.error(err.message);
// 打印: The message

The error.message property is the string description of the error as set by calling new Error(message). The message passed to the constructor will also appear in the first line of the stack trace of the Error, however changing this property after the Error object is created may not change the first line of the stack trace (for example, when error.stack is read before this property is changed).

const err = new Error('The message');
console.error(err.message);
// Prints: The message