JS元编程 – 获取函数声明的行

通常我可以使用new Error().stack来获取运行时path所在的path。 但是,在我目前的情况下,我无法得到堆栈跟踪,因为如果我声明new Error()它将是在错误的path。

不过,我确实有一个参考我想跟踪的function。 我真正想要的是该函数声明的行号。 是否有可能使用一些JavaScript元编程设施或new Error().stack的唯一方法?

这里是我正在谈论的代码:

 function handleTest(test, cb) { function makeCallback(err){ clearTimeout(timer); handleTestResult(err, test); cb(null, err); } var done = _.once(makeCallback); try { if(!inDebugMode){ //only add timeout if we aren't debugging var timer = setTimeout(function () { test.timedOut = true; done(new Error('timed out'); // the stack trace for this error will not include the info I need }, 2000); } test.cb.apply(self, [function (err) { //what I really want is the location of the test.cb function, (where test.cb is declared) done(err); }]); } catch (err) { //this err instance on the other hand has a stack trace I can use console.log(err.stack); done(err); } }