获取匿名函数名 [英] Get anonymous function name

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

问题描述

本例中如何从函数中获取变量名:

How to get the the variable name from within a function in this example:

// it should return A
var A = function(){ console.log(this.name); } 

有这样的吗?

推荐答案

那个函数是匿名的;它没有名字.但是,您可以为其命名:

That function is anonymous; it has no name. You could, however, give it a name:

var A = function a() {};

然后它的名字可以通过 Function 访问.name:

Then its name is accessible via Function.name:

var A = function a() {};
A.name
> 'a'

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

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