How to get the function name from within that function?

前端 未结 20 1232
抹茶落季
抹茶落季 2020-11-22 16:06

How can I access a function name from inside that function?

// parasitic inheritance
var ns.parent.child = function() {
  var parent = new ns.parent();
  p         


        
20条回答
  •  半阙折子戏
    2020-11-22 16:34

    You could use this, for browsers that support Error.stack (not nearly all, probably)

    function WriteSomeShitOut(){ 
      var a = new Error().stack.match(/at (.*?) /);
      console.log(a[1]);
    } 
    WriteSomeShitOut();
    

    of course this is for the current function, but you get the idea.

    happy drooling while you code

提交回复
热议问题