util.getCallSites(frameCountOrOptions, [options])
¥Stability: 1.1 - Active development
-
frameCount
<number> 要捕获为调用站点对象的可选帧数。默认值:10
。允许的范围在 1 到 200 之间。¥
frameCount
<number> Optional number of frames to capture as call site objects. Default:10
. Allowable range is between 1 and 200. -
options
<Object> 可选的¥
options
<Object> Optional -
返回:<Object[]> 调用站点对象数组
¥Returns: <Object[]> An array of call site objects
-
functionName
<string> 返回与此调用站点关联的函数的名称。¥
functionName
<string> Returns the name of the function associated with this call site. -
scriptName
<string> 返回包含此调用站点的函数脚本的资源的名称。¥
scriptName
<string> Returns the name of the resource that contains the script for the function for this call site. -
scriptId
<string> 返回脚本的唯一 ID,如 Chrome DevTools 协议Runtime.ScriptId
中所述。¥
scriptId
<string> Returns the unique id of the script, as in Chrome DevTools protocolRuntime.ScriptId
. -
lineNumber
<number> 返回 JavaScript 脚本行号(从 1 开始)。¥
lineNumber
<number> Returns the JavaScript script line number (1-based). -
columnNumber
<number> 返回 JavaScript 脚本列号(从 1 开始)。¥
columnNumber
<number> Returns the JavaScript script column number (1-based).
-
返回包含调用者函数堆栈的调用站点对象数组。
¥Returns an array of call site objects containing the stack of the caller function.
const util = require('node:util');
function exampleFunction() {
const callSites = util.getCallSites();
console.log('Call Sites:');
callSites.forEach((callSite, index) => {
console.log(`CallSite ${index + 1}:`);
console.log(`Function Name: ${callSite.functionName}`);
console.log(`Script Name: ${callSite.scriptName}`);
console.log(`Line Number: ${callSite.lineNumber}`);
console.log(`Column Number: ${callSite.columnNumber}`);
});
// CallSite 1:
// Function Name: exampleFunction
// Script Name: /home/example.js
// Line Number: 5
// Column Number: 26
// CallSite 2:
// Function Name: anotherFunction
// Script Name: /home/example.js
// Line Number: 22
// Column Number: 3
// ...
}
// A function to simulate another stack layer
function anotherFunction() {
exampleFunction();
}
anotherFunction();
可以通过将选项 sourceMap
设置为 true
来重建原始位置。如果源映射不可用,则原始位置将与当前位置相同。启用 --enable-source-maps
标志时,例如使用 --experimental-transform-types
时,sourceMap
默认为 true。
¥It is possible to reconstruct the original locations by setting the option sourceMap
to true
.
If the source map is not available, the original location will be the same as the current location.
When the --enable-source-maps
flag is enabled, for example when using --experimental-transform-types
,
sourceMap
will be true by default.
import util from 'node:util';
interface Foo {
foo: string;
}
const callSites = util.getCallSites({ sourceMap: true });
// With sourceMap:
// Function Name: ''
// Script Name: example.js
// Line Number: 7
// Column Number: 26
// Without sourceMap:
// Function Name: ''
// Script Name: example.js
// Line Number: 2
// Column Number: 26