从对象中获取对象名称 [英] get object name from within object

查看:88
本文介绍了从对象中获取对象名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

对象/函数。


函数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.


这篇关于从对象中获取对象名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆