代码如下:
有个缺点: 如果不是通过function A(){}这种方式定义的函数取不到函数名称
js 代码
- function getCallStack(){
- var fn, stack;
- if(arguments.length != 2){
- fn = arguments.callee.caller;
- stack = [];
- }else{
- fn = arguments[0];
- stack = arguments[1];
- }
- var fCaller;
- if(fn == null){
- fCaller = "(Top Caller)";
- stack.unshift(fCaller);
- return stack;
- }else{
- var params = /(function\s*([^\(]*)\s*\((.*)\))/.exec(fn.toString());
- if(params != null){
- fCaller = params[1];//"function " + params[1] + "(" + params[2] + ")";
- }else{
- var fnn = fn.toString();
- fnn = fnn.substring(0, fnn.indexOf("\n")==-1 ? 25 : fnn.indexOf("\n"));
- fCaller = "Unknown Caller: " + fnn;
- }
- }
- stack.unshift(fCaller);
- if(fn != null){
- arguments.callee(fn.caller, stack);
- }
- return stack;
- }
有个缺点: 如果不是通过function A(){}这种方式定义的函数取不到函数名称