Get anonymous function name

前端 未结 4 1039
深忆病人
深忆病人 2020-12-06 21:00

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); } 
4条回答
  •  甜味超标
    2020-12-06 21:22

    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'
    

提交回复
热议问题