从对象中获取对象名称

从对象中获取对象名称

问题描述:

有没有办法从里面找出对象/函数名称

对象/函数。


函数coffee(){

alert(this.someHowGetMyNameFuncOrVar); =>应该给我''咖啡''

}


var milk = new coffee(); =>应该给我''牛奶''

is there a way to find out the object/function name from inside
object/function.

function coffee(){
alert(this.someHowGetMyNameFuncOrVar); => should give me ''coffee''
}

var milk = new coffee(); => should give me ''milk''




warteschlange写道:


warteschlange wrote:
有没有办法从对象/函数中找出对象/函数名称。
is there a way to find out the object/function name from inside
object/function.




里面的你可以访问的函数

arguments.callee

这是被调用的函数。使用Mozilla JavaScript函数有一个

名称属性,所以你可以访问

arguments.callee.name

获取函数名称但其他浏览器/ implementation没有
实现那个name属性所以如果你需要函数名,那么你需要尝试从函数的源代码中解析它br />
表示,即你需要解析

arguments.callee.toString()

的函数名。

-


Martin Honnen
http:/ /JavaScript.FAQTs.com/


我想这对匿名函数来说会有问题。


var myFunction = function(){};

I guess that would be a problem for anonymous functions.

var myFunction=function(){};


对不起,这是一个明显的陈述。

Sorry, that was a statement of the obvious.