Error.prototype.stack

Non-standard: This feature is non-standard and is not on a standards track. Do not use it on production sites facing the Web: it will not work for every user. There may also be large incompatibilities between implementations and the behavior may change in the future.

注意:stack 属性实际上由所有主要 JavaScript 引擎实现,而 JavaScript 标准委员会正在寻求标准化。由于实现不一致,你不能依赖堆栈字符串的精确内容,但你通常可以假设它存在并将其用于调试目的。

¥Note: The stack property is de facto implemented by all major JavaScript engines, and the JavaScript standards committee is looking to standardize it. You cannot rely on the precise content of the stack string due to implementation inconsistencies, but you can generally assume it exists and use it for debugging purposes.

Error 实例的非标准 stack 属性提供了对哪些函数被调用、以什么顺序、从哪一行和文件以及使用什么参数调用的跟踪。堆栈字符串从最近的调用继续到较早的调用,返回到原始的全局范围调用。

¥The non-standard stack property of an Error instance offers a trace of which functions were called, in what order, from which line and file, and with what arguments. The stack string proceeds from the most recent calls to earlier ones, leading back to the original global scope call.

¥Value

一根绳子。

¥A string.

由于 stack 属性是非标准的,因此其安装位置的实现有所不同。

¥Because the stack property is non-standard, implementations differ about where it's installed.

  • 在 Firefox 中,它是 Error.prototype 上的访问器属性。
  • 在 Chrome 和 Safari 中,它是每个 Error 实例上的数据属性,其描述符为:
Property attributes of Error.prototype.stack
Writable yes
Enumerable no
Configurable yes

描述

¥Description

每个 JavaScript 引擎都使用自己的堆栈跟踪格式,但它们的高级结构相当一致。每个实现都使用堆栈中的单独一行来表示每个函数调用。直接导致错误的调用放在顶部,启动整个调用链的调用放在底部。以下是堆栈跟踪的一些示例:

¥Each JavaScript engine uses its own format for stack traces, but they are fairly consistent in their high-level structure. Every implementation uses a separate line in the stack to represent each function call. The call that directly caused the error is placed at the top, and the call that started the whole call chain is placed at the bottom. Below are some examples of stack traces:

js
function foo() {
  bar();
}

function bar() {
  baz();
}

function baz() {
  console.log(new Error().stack);
}

foo();
#### JavaScriptCore
baz@filename.js:10:24
bar@filename.js:6:6
foo@filename.js:2:6
global code@filename.js:13:4

#### SpiderMonkey
baz@filename.js:10:15
bar@filename.js:6:3
foo@filename.js:2:3
@filename.js:13:1

#### V8
Error
    at baz (filename.js:10:15)
    at bar (filename.js:6:3)
    at foo (filename.js:2:3)
    at filename.js:13:1

不同的引擎在不同的时间设置该值。大多数现代引擎在创建 Error 对象时设置它。这意味着你可以使用以下命令获取函数内的完整调用堆栈信息:

¥Different engines set this value at different times. Most modern engines set it when the Error object is created. This means you can get the full call stack information within a function using the following:

js
function foo() {
  console.log(new Error().stack);
}

不必抛出错误然后捕获它。

¥Without having to throw an error and then catch it.

在 V8 中,可以使用非标准 Error.captureStackTrace()Error.stackTraceLimitError.prepareStackTrace() API 来自定义堆栈跟踪。阅读 V8 文档中的 堆栈跟踪 API 了解更多信息。

¥In V8, the non-standard Error.captureStackTrace(), Error.stackTraceLimit, and Error.prepareStackTrace() APIs can be used to customize the stack trace. Read the Stack trace API in the V8 docs for more information.

堆栈帧也可以是显式函数调用以外的东西。例如,事件监听器、超时作业和 Promise 处理程序都开始自己的调用链。eval()Function 构造函数调用中的源代码也出现在堆栈中:

¥Stack frames can be things other than explicit function calls, too. For example, event listeners, timeout jobs, and promise handlers all begin their own call chain. Source code within eval() and Function constructor calls also appear in the stack:

js
console.log(new Function("return new Error('Function failed')")().stack);
console.log("====");
console.log(eval("new Error('eval failed')").stack);
#### JavaScriptCore
anonymous@
global code@filename.js:1:65
====
eval code@
eval@[native code]
global code@filename.js:3:17

#### SpiderMonkey
anonymous@filename.js line 1 > Function:1:8
@filename.js:1:65

====
@filename.js line 3 > eval:1:1
@filename.js:3:13

#### V8
Error: Function failed
    at eval (eval at <anonymous> (filename.js:1:13), <anonymous>:1:8)
    at filename.js:1:65
====
Error: eval failed
    at eval (eval at <anonymous> (filename.js:3:13), <anonymous>:1:1)
    at filename.js:3:13

在 Firefox 中,你可以使用 //# sourceURL 指令来命名评估源。有关更多详细信息,请参阅 Firefox 调试评估源 文档和 使用 //# sourceURL 指令命名 eval 脚本 博客文章。

¥In Firefox, you can use the //# sourceURL directive to name an eval source. See the Firefox Debug eval sources docs and the Naming eval Scripts with the //# sourceURL Directive blog post for more details.

示例

¥Examples

使用堆栈属性

¥Using the stack property

以下脚本演示了如何使用 stack 属性将堆栈跟踪输出到浏览器窗口中。你可以使用它来检查浏览器的堆栈结构。

¥The following script demonstrates how to use the stack property to output a stack trace into your browser window. You can use this to check what your browser's stack structure looks like.

html
<div id="output"></div>
css
#output {
  white-space: pre;
  font-family: monospace;
}
js
function trace() {
  throw new Error("trace() failed");
}
function b() {
  trace();
}
function a() {
  b(3, 4, "\n\n", undefined, {});
}
try {
  a("first call, firstarg");
} catch (e) {
  document.getElementById("output").textContent = e.stack;
}

规范

¥Specifications

不属于任何标准。

¥Not part of any standard.

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看

¥See also