How to get the function name from within that function?

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

    You could use Function.name:

    In most implementations of JavaScript, once you have your constructor's reference in scope, you can get its string name from its name property (e.g. Function.name, or Object.constructor.name

    You could use Function.callee:

    The native arguments.caller method has been deprecated, but most browsers support Function.caller, which will return the actual invoking object (its body of code): https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/caller?redirectlocale=en-US&redirectslug=JavaScript%2FReference%2FGlobal_Objects%2FFunction%2Fcaller

    You could create a source map:

    If what you need is the literal function signature (the "name" of it) and not the object itself, you might have to resort to something a little more customized, like creating an array reference of the API string values you'll need to access frequently. You can map them together using Object.keys() and your array of strings, or look into Mozilla's source maps library on GitHub, for bigger projects: https://github.com/mozilla/source-map

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

    You can use constructor name like:

    {your_function}.prototype.constructor.name
    

    this code simply return name of a method.

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

    Any constructor exposes a property name, which is the function name. You access the constructor via an instance (using new) or a prototype:

    function Person() {
      console.log(this.constructor.name); //Person
    }
    
    var p = new Person();
    console.log(p.constructor.name); //Person
    
    console.log(Person.prototype.constructor.name);  //Person
    
    0 讨论(0)
  • 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

    0 讨论(0)
  • 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.

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

    This worked for me.

    function AbstractDomainClass() {
        this.className = function() {
            if (!this.$className) {
                var className = this.constructor.toString();
                className = className.substr('function '.length);
                className = className.substr(0, className.indexOf('('));
                this.$className = className;
            }
            return this.$className;
        }
    }
    

    Test code:

    var obj = new AbstractDomainClass();
    expect(obj.className()).toBe('AbstractDomainClass');
    
    0 讨论(0)
提交回复
热议问题