javascript - How to get the function name from within that function? -


how can access function name inside function?

// parasitic inheritance var ns.parent.child = function() {   var parent = new ns.parent();   parent.newfunc = function() {    }   return parent; }  var ns.parent = function() {   // @ point, want know child called parent   // ie }  var obj = new ns.parent.child(); 

in es5, best thing is:

function functionname(fun) {   var ret = fun.tostring();   ret = ret.substr('function '.length);   ret = ret.substr(0, ret.indexof('('));   return ret; } 

using function.caller non-standard , arguments.callee forbidden in strict mode.

edit: nus's regex based answer below achieves same thing, has better performance!

in es6, can use myfunction.name.

note: beware js minifiers might throw away function names, compress better; may need tweak settings avoid that.


Comments

Popular posts from this blog

jQuery Mobile app not scrolling in Firefox -

c++ - How to add Crypto++ library to Qt project -

php array slice every 2th rule -