当启用 use strict 时,如何找出 JavaScript 中的调用者函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29572466/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you find out the caller function in JavaScript when use strict is enabled?
提问by Jamie Hutber
Is it possible to see the callee/caller of a function when use strictis enabled?
use strict启用时是否可以看到函数的被调用者/调用者?
'use strict';
function jamie (){
console.info(arguments.callee.caller.name);
//this will output the below error
//uncaught TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them
};
function jiminyCricket (){
jamie();
}
jiminyCricket ();
回答by p.s.w.g
For what it's worth, I agree with the comments above. For whatever problem you're trying to solve, there are usually better solutions.
对于它的价值,我同意上面的评论。对于您要解决的任何问题,通常都有更好的解决方案。
However, just for illustrative purposes, here's one (very ugly) solution:
但是,仅出于说明目的,这里有一个(非常丑陋的)解决方案:
'use strict'
function jamie (){
var callerName;
try { throw new Error(); }
catch (e) {
var re = /(\w+)@|at (\w+) \(/g, st = e.stack, m;
re.exec(st), m = re.exec(st);
callerName = m[1] || m[2];
}
console.log(callerName);
};
function jiminyCricket (){
jamie();
}
jiminyCricket(); // jiminyCricket
I've only tested this in Chrome, Firefox, and IE11, so your mileage may vary.
我只在 Chrome、Firefox 和 IE11 中测试过这个,所以你的里程可能会有所不同。
回答by inetphantom
Please note that this should not be used for productive purposes. This is an ugly solution, which can be helpful for debugging, but if you need something from the caller, pass it as argument or save it into a accessible variable.
请注意,这不应用于生产目的。这是一个丑陋的解决方案,它对调试很有帮助,但如果您需要调用者的某些东西,请将其作为参数传递或将其保存到可访问的变量中。
The short version of @p.s.w.g answer(without throwing an error, just instantiating one):
@pswg 答案的简短版本(没有抛出错误,只是实例化一个):
let re = /([^(]+)@|at ([^(]+) \(/g;
let aRegexResult = re.exec(new Error().stack);
sCallerName = aRegexResult[1] || aRegexResult[2];
Full Snippet:
完整片段:
'use strict'
function jamie (){
var sCallerName;
{
let re = /([^(]+)@|at ([^(]+) \(/g;
let aRegexResult = re.exec(new Error().stack);
sCallerName = aRegexResult[1] || aRegexResult[2];
}
console.log(sCallerName);
};
function jiminyCricket(){
jamie();
};
jiminyCricket(); // jiminyCricket
回答by Benamar
It does not worked for me Here is what I finally do, just in case it helps someone
它对我不起作用这是我最终做的,以防万一它对某人有帮助
function callerName() {
try {
throw new Error();
}
catch (e) {
try {
return e.stack.split('at ')[3].split(' ')[0];
} catch (e) {
return '';
}
}
}
function currentFunction(){
let whoCallMe = callerName();
console.log(whoCallMe);
}
回答by Larry
You can get a stack trace using:
您可以使用以下方法获取堆栈跟踪:
console.trace()
but this is likely not useful if you need to do something with the caller.
但是如果您需要对呼叫者做某事,这可能没有用。
See https://developer.mozilla.org/en-US/docs/Web/API/Console/trace
请参阅https://developer.mozilla.org/en-US/docs/Web/API/Console/trace
回答by toddmo
functionName() {
return new Error().stack.match(/ at (\S+)/g)[1].get(/ at (.+)/);
}
// Get - extract regex
String.prototype.get = function(pattern, defaultValue = "") {
if(pattern.test(this)) {
var match = this.match(pattern);
return match[1] || match[0];
}
return defaultValue; // if nothing is found, the answer is known, so it's not null
}

