分析arguments.callee或func_name.callerfunction func_name() {
alert(func_name.caller);
}function foo() {
alert(arguments.callee);
func_name();
}
foo();
alert(func_name.caller);
}function foo() {
alert(arguments.callee);
func_name();
}
foo();
functionname.aller 这种知识是从哪学的?是java吗?怎么我看了这么多的javascript文章都没有提到过的?应该是找哪些书看啊?
我觉得楼主可以写个自定义一个方法,来显示函数名称
var s = this.toString();
var m = s.match(/function\s+([^(]+)/);
return m[1];
}
function foo(sdfsdf){
alert(arguments.callee.getName());
}
var fstrings = arguments.callee.toString();
this.name = fstrings.substring(9, fstrings.indexOf("("));
alert(this.name);
}fooffffff();试了,要是做成
Function.prototype.name = function () {
}
的形式就更好了,可惜我没有成功,因为arguments的关系。
这样写就可以了Function.prototype.getName = function() {
var s = Function.prototype.getName.caller.toString();
var m = s.match(/function\s+(\w+)/);
return m[1];
}function foo (sdfsdf){
alert(Function.getName());
}
foo();
{
//这个该如何取?
}
改成这样:
Function.prototype.getName = function() {
var s = Function.prototype.getName.caller.toString();
return s.split(/[\r\n]+/)[0].replace(/function\s+(\w+).+/g, '$1');
}var foo = function(e)
{
//这个该如何取?
alert(Function.getName()); // 得到function(e),本来就没有名字嘛
}
<SCRIPT LANGUAGE="JavaScript">
<!--
Function.prototype.getName = function() {
var s = Function.prototype.getName.caller.toString();
return /function\s+([^\s\(]+)/i.test(s) ? RegExp.$1 : "anonymous";
}var mm=new Function("alert(Function.getName())");function 梅花雪(){alert(Function.getName());}var foo = function(e)
{
alert(Function.getName());
mm();
梅花雪();
}
foo();
//-->
</SCRIPT>