Get anonymous function name

前端 未结 4 1033
深忆病人
深忆病人 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:32

    I know this is an old thread, but still in search results. so just for reference:

    a solution could simply be using the stacktrace.

    var stack = new Error().stack;
    

    use trim and split to get to the desired values.

提交回复
热议问题