Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get anonymous function name

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); } 

Is there something like this?

like image 430
Adam Halasz Avatar asked Dec 01 '22 04:12

Adam Halasz


1 Answers

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

var A = function a() {};

Then its name is accessible via Function.name:

var A = function a() {};
A.name
> 'a'
like image 120
Ross Allen Avatar answered Dec 02 '22 17:12

Ross Allen