function main()
{
Hello();
}
function Hello()
{
// How do you find out the caller function is 'main'?
}
有办法找到调用堆栈吗?
function main()
{
Hello();
}
function Hello()
{
// How do you find out the caller function is 'main'?
}
有办法找到调用堆栈吗?
当前回答
概括一下(并说得更清楚)…
这段代码:
function Hello() {
alert("caller is " + arguments.callee.caller.toString());
}
等价于:
function Hello() {
alert("caller is " + Hello.caller.toString());
}
显然,第一部分更容易移植,因为你可以改变函数的名字,从“Hello”说成“Ciao”,但仍然可以让整个程序正常工作。
在后一种情况下,如果你决定重构被调用函数的名称(Hello),你将不得不改变它的所有出现:
其他回答
注意,不能使用Function。Node.js中的caller,使用caller-id包代替。例如:
var callerId = require('caller-id');
function foo() {
bar();
}
function bar() {
var caller = callerId.getData();
/*
caller = {
typeName: 'Object',
functionName: 'foo',
filePath: '/path/of/this/file.js',
lineNumber: 5,
topLevelFlag: true,
nativeFlag: false,
evalFlag: false
}
*/
}
function main()
{
Hello();
}
function Hello()
{
new Error().stack
}
2018年更新
严格模式下禁止调用。下面是使用(非标准)错误堆栈的替代方案。
下面的函数似乎在Firefox 52和Chrome 61-71中完成了这项工作,尽管它的实现对这两种浏览器的日志格式做了很多假设,应该谨慎使用,因为它会抛出一个异常,并可能在完成之前执行两个正则表达式匹配。
'use strict'; const fnNameMatcher = /([^(]+)@|at ([^(]+) \(/; function fnName(str) { const regexResult = fnNameMatcher.exec(str); return regexResult[1] || regexResult[2]; } function log(...messages) { const logLines = (new Error().stack).split('\n'); const callerName = fnName(logLines[1]); if (callerName !== null) { if (callerName !== 'log') { console.log(callerName, 'called log with:', ...messages); } else { console.log(fnName(logLines[2]), 'called log with:', ...messages); } } else { console.log(...messages); } } function foo() { log('hi', 'there'); } (function main() { foo(); }());
你可以使用调试器;在功能上。 打开inspect元素和watch调用堆栈;
概括一下(并说得更清楚)…
这段代码:
function Hello() {
alert("caller is " + arguments.callee.caller.toString());
}
等价于:
function Hello() {
alert("caller is " + Hello.caller.toString());
}
显然,第一部分更容易移植,因为你可以改变函数的名字,从“Hello”说成“Ciao”,但仍然可以让整个程序正常工作。
在后一种情况下,如果你决定重构被调用函数的名称(Hello),你将不得不改变它的所有出现: