以编程方式访问功能位置

Sus*_*uth 19 google-chrome google-chrome-devtools console.log

是否可以在代码中访问["[[FunctionLocation]]"]google chrome开发人员工具在使用控制台日志功能时显示的属性?

Tim*_* Gu 25

答案,现在,答案是否定的.

[[FunctionLocation]]您在Inspector中看到的属性将添加V8Debugger::internalProperties()到调试器的C++代码中,该代码使用另一个C++函数V8Debugger::functionLocation()来收集有关该函数的信息.functionLocation()然后使用许多特定于V8的C++ API,例如v8::Function::GetScriptLineNumber()GetScriptColumnNumber()找出确切的信息.

上述所有API仅适用于C++代码,而不适用于JavaScript代码.如果您尝试在Node.js这样的平台上完成此任务,那么您应该能够编写本机模块.如果没有,那你就不走运了.


小智 12

我知道这个问题发布已经有一段时间了。现在,我遇到了同样的问题。我需要访问运行时的函数位置。

幸运的是,NodeJS 通过模块暴露了一些 v8 内部属性,做得很好inspector

我编写了一个名为 的小模块func-loc,它有助于检索运行时上的函数位置。

例子:

const { locate } = require('func-loc');

const fn = () => {
  console.log('Hello there');
};

(async () => {
  const result = await locate(fn);
  console.log(result);
  // Will result: { source: 'file://__BASE_FOLDER__/func-loc/this-file.js', line: 3, column: 12 }
})();
Run Code Online (Sandbox Code Playgroud)

希望有帮助。