获取函数名

 

 

 

取出函数的名称      

var getFnName = function(callee){
 //将函数中的空格全都去掉
 var _callee = callee.toString().replace(/[\s\?]*/g,"");
 comb = _callee.length >= 50 ? 50 :_callee.length;
 alert( _callee );
 _callee = _callee.substring(0,comb);
 alert( _callee );
 var name = _callee.match(/^function([^\(]+?)\(/);
 alert( name );
 //返回匹配的数组    name[1]返回第一个匹配的字表达式的结果
 if(name && name[1]){
   return name[1];
 }
 var caller = callee.caller,
 _caller = caller.toString().replace(/[\s\?]*/g,"");
 var last = _caller.indexOf(_callee),
 str = _caller.substring(last-30,last);
 name = str.match(/var([^\=]+?)\=/);
 if(name && name[1]){
   return name[1];
 }
 return "anonymous"
};
 
function  ee(){
 //+++++++++++++++++++++++++++++++++
 var fnname =getFnName(arguments.callee)   
 //+++++++++++++++++++++++++++++++++
 alert(fnname)
 };
ee();

 

你可能感兴趣的:(函数)