How to get the function name from within that function?

前端 未结 20 1237
抹茶落季
抹茶落季 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:29

    I had a similar problem and I solved it as follows:

    Function.prototype.myname = function() {
       return this.toString()
           .substr( 0, this.toString().indexOf( "(" ) )
           .replace( "function ", "" ); 
    }
    

    This code implements, in a more comfortable fashion, one response I already read here at the top of this discussion. Now I have a member function retrieving the name of any function object. Here's the full script ...

    
    

提交回复
热议问题