可选
cause可选
stack静态
stackError.stackTraceLimit
属性指定了堆栈跟踪(无论是通过 new Error().stack
生成还是通过 Error.captureStackTrace(obj)
生成)收集的堆栈帧数。
默认值为 10
但可以设置为任何有效的 JavaScript 数字。更改将影响值更改之后捕获的任何堆栈跟踪。
如果设置为非数字值,或设置为负数,堆栈跟踪将不会捕获任何帧。
静态
capture在 targetObject
上创建一个 .stack
属性,当访问时,该属性将返回一个字符串,表示在代码中调用 Error.captureStackTrace()
的位置。
const myObject = {};
Error.captureStackTrace(myObject);
myObject.stack; // Similar to `new Error().stack`
跟踪的第一行将以 ${myObject.name}: ${myObject.message}
为前缀。
可选的 constructorOpt
参数接受一个函数。如果提供了该参数,则生成堆栈跟踪时将省略 constructorOpt
以上的所有帧,包括 constructorOpt
本身。
constructorOpt
参数对于向用户隐藏错误生成的实现细节很有用。例如
function a() {
b();
}
function b() {
c();
}
function c() {
// Create an error without stack trace to avoid calculating the stack trace twice.
const { stackTraceLimit } = Error;
Error.stackTraceLimit = 0;
const error = new Error();
Error.stackTraceLimit = stackTraceLimit;
// Capture the stack trace above function b
Error.captureStackTrace(error, b); // Neither function c, nor b is included in the stack trace
throw error;
}
a();
可选
constructorOpt: Function静态
prepare
当提供了无效命名空间时抛出的错误。