How to get the function name from within that function?

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

    Easy way to get function name from within fuction you are running.

    function x(){alert(this.name)};x()

    0 讨论(0)
  • 2020-11-22 16:46

    I know this is a old question but lately I've been facing some similar issue while trying to decorate some React Component's methods, for debugging purposes. As people already said, arguments.caller and arguments.callee are forbidden in strict mode which is probably enabled by default in your React transpiling. You can either disable it, or I've been able to come up with another hack, because in React all class functions are named, you can actually do this:

    Component.prototype.componentWillMount = function componentWillMount() {
        console.log('Callee name: ', this.__proto__.constructor.toString().substr(0,30));
    ...
    }
    
    0 讨论(0)
提交回复
热议问题