本文主要介绍了Javascript获取函数名称、函数参数以及对象属性的代码,其中包括了获取函数名称的3种实现方法和用JS获取函数的所有参数并且遍历某个对象所有属性的名称和值的方法。
获取函数名称的3种实现方法
1.在js权威指南中看到的一个方法:
Function.prototype.getName = function(){
return this.name || this.toString().match(/function\s*([^(]*)\(/)[1]
}
2.如果当前函数是有名函数,则返回其名字,如果是匿名函数则返回被赋值的函数变量名,如果是闭包中匿名函数则返回“anonymous”,代码如下:
var getFnName = function(callee){
var _callee = callee.toString().replace(/[\s\?]*/g,""),
comb = _callee.length >= 50 ? 50 :_callee.length;
_callee = _callee.substring(0,comb);
var name = _callee.match(/^function([^\(]+?)\(/);
if(name && name[1]){
return name[1];
}
var caller = callee.caller,
_caller = caller.toString().replace(/[\s\?]*/g,"")