如何在 JavaScript 中找到调用函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8211264/
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 can I find the calling function in JavaScript?
提问by testndtv
I have a function in JS which is getting called from multiple places..
我在 JS 中有一个函数,它从多个地方被调用。
Now I am testing this page on an iPad and hence find debugging a bit hard.
现在我正在 iPad 上测试这个页面,因此发现调试有点困难。
Can I find out by some way (print on console) from where my function is getting called from ?
我可以通过某种方式(在控制台上打印)从我的函数被调用的地方找到吗?
回答by James
Like this?
像这样?
function testOne() {
console.log("Test 1");
logTest();
}
function testTwo() {
console.log("Test 2");
logTest();
}
function logTest() {
console.log("Being called from " + arguments.callee.caller.toString());
}
testOne();
testTwo();
If you use 'use strict';
in your JavaScript file, you need to comment/remove it, because otherwise you'll get something like this:
如果你'use strict';
在你的 JavaScript 文件中使用,你需要注释/删除它,否则你会得到这样的东西:
Uncaught TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them
未捕获的 TypeError: 'caller'、'callee' 和 'arguments' 属性可能无法在严格模式函数或调用它们的参数对象上访问
回答by SquareFeet
A simple way I like to use is arguments.callee.caller.name
.
我喜欢使用的一种简单方法是arguments.callee.caller.name
.
Say you wanted to know what was calling a function called myFunction:
假设您想知道是什么调用了名为 myFunction 的函数:
function myFunction() {
console.log(arguments.callee.caller.name);
/* Other stuff... */
}
The browser support for this isn't that great, though, so you could use arguments.callee.caller.toString() instead. Note that this will give you back the contents of the function that called myFunction, so you will need to dig out the function name from that yourself.
不过,浏览器对此的支持并不是那么好,因此您可以改用 arguments.callee.caller.toString()。请注意,这将返回调用 myFunction 的函数的内容,因此您需要自己从中挖掘出函数名称。
Or, you could use a nice stack trace function like this:
或者,您可以使用一个不错的堆栈跟踪函数,如下所示:
function getStack(){
fnRE = /function\s*([\w\-$]+)?\s*\(/i;
var caller = arguments.callee.caller;
var stack = "Stack = ";
var fn;
while (caller){
fn = fnRE.test(caller.toString()) ? RegExp. || "{?}" : "{?}";
stack += "-->"+fn;
caller = caller.arguments.callee.caller;
};
return stack;
}
Stack Trace via http://www.amirharel.com/2010/01/25/using-caller-and-callee-for-stack-trace/
通过http://www.amirharel.com/2010/01/25/using-caller-and-callee-for-stack-trace/ 进行堆栈跟踪
回答by Nishant Kumar
Want to Detail about caller Function:
想要详细了解调用者功能:
function nishant(){ // Caller function
kumar();
}nishant();
function kumar(){ // Callee
console.log("This functiona is being called by " + arguments.callee.caller.toString());
}
In place of arguments.callee.caller.toString()
we can also use functionName.caller
代替arguments.callee.caller.toString()
我们也可以使用functionName.caller
Example:
例子:
function nishant(){ // Caller function
kumar();
}nishant();
function kumar(){ // Callee
console.log("This functiona is being called by " + kumar.caller);
}
Output: will be same in both of the above case
输出:在上述两种情况下都相同
This functiona is being called by function nishant()
{
kumar();
}
回答by Dheeraj
Do not use function.caller it is non standardand is not recommended.
不要使用函数.caller 它是非标准的,不推荐使用。
Instead use the browser console and put break points on your function check the call stack, Here is a screenshot of chrome debugger console
而是使用浏览器控制台并在您的函数上放置断点检查调用堆栈,这是 chrome 调试器控制台的屏幕截图