How to get the function name from within that function?

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

    what you're doing is assigning unnamed function to a variable. you probably need named function expression instead ( http://kangax.github.com/nfe/ ).

    var x = function x() {
        console.log( arguments.callee.name );
    }
    x();
    

    however I'm not sure how much cross-browser that is; there's an issue with IE6 that makes you function's name leak to the outer scope. also, arguments.callee is kind of deprecated and will result in error if you're using strict mode.

提交回复
热议问题