目前的答案是no.
The [[FunctionLocation]]
您在 Inspector 中看到的属性已添加到V8Debugger::internalProperties() https://github.com/v8/v8/blob/f9fbaec39a201812c5e45c9b632f012ab3c531b7/src/inspector/v8-debugger.cc#L719-L721在调试器的 C++ 代码中,它使用另一个 C++ 函数V8Debugger::functionLocation() https://github.com/v8/v8/blob/f9fbaec39a201812c5e45c9b632f012ab3c531b7/src/inspector/v8-debugger.cc#L831-L861收集有关该功能的信息。functionLocation()
然后使用许多 V8 特定的 C++ API,例如v8::Function::GetScriptLineNumber() and GetScriptColumnNumber() https://github.com/v8/v8/blob/f9fbaec39a201812c5e45c9b632f012ab3c531b7/include/v8.h#L3685-L3694找出确切的信息。
上述所有 API 仅供 C++ 代码使用,而非 JavaScript 代码。换句话说,网页上的 JavaScript 代码无法直接访问此信息。
但是,您也许可以使用 Chrome 扩展程序来访问这些属性。最近,Chrome 使用的 V8 JavaScript 引擎添加了通过以下方式访问这些属性的支持:Chrome 开发者工具协议 https://chromedevtools.github.io/devtools-protocol/。特别是,您可以通过以下方式获取内部属性Runtime.getProperties https://chromedevtools.github.io/devtools-protocol/tot/Runtime#method-getProperties称呼。此外,它似乎 https://chromedevtools.github.io/devtools-protocol/#extensionChrome 扩展程序可以通过以下方式与 DevTools 协议进行交互chrome.debugger https://developer.chrome.com/extensions/debugger.html.
在 Node.js 中使用 DevTools 协议的概念证明,它可以使用督察 https://nodejs.org/api/inspector.html内置模块(穆罕默德在他们的回答中提到):
global.a = () => { /* test function */ };
const s = new (require('inspector').Session)();
s.connect();
let objectId;
s.post('Runtime.evaluate', { expression: 'a' }, (err, { result }) => {
objectId = result.objectId;
});
s.post('Runtime.getProperties', { objectId }, (err, { internalProperties }) => {
console.log(internalProperties);
});
yields
[
{
name: '[[FunctionLocation]]',
value: {
type: 'object',
subtype: 'internal#location',
value: [Object],
description: 'Object'
}
},
{
name: '[[Scopes]]',
value: {
type: 'object',
subtype: 'internal#scopeList',
className: 'Array',
description: 'Scopes[2]',
objectId: '{"injectedScriptId":1,"id":24}'
}
}
]
使用 Node.js v12.3.1。