Get method name from within a typescript method

I was looking for a solution as well, try this:

class Foo {
  bar() {
      console.log(this.bar.name); // <-- Print out the function name.
  }
}
  
new Foo().bar(); 

What is nice is that you'll get an error if you change the function name, but forget to update the console statement.


Besides the arguments.callee.name there is no straightforward way of getting this.

I propose 2 other methods:

Use decorators to inject the method name:

function annotateName(target, name, desc) {
    var method = desc.value;
    desc.value = function () {
        var prevMethod = this.currentMethod;
        this.currentMethod = name;
        method.apply(this, arguments);
        this.currentMethod = prevMethod;   
    }
}

class Foo {
    currentMethod: string;

    @annotateName
    bar() {
        alert(this.currentMethod);
        this.tux();
        alert(this.currentMethod);
    }

    @annotateName
    tux() {
        alert(this.currentMethod);
    }
}

new Foo().bar();

The downside is that you have to annotate all the functions you want to get the name from. You could instead just annotate the class and in the decorator you would iterate over all prototype functions and apply the same idea.


My second option is not standardised and need more care to get consistent results across browsers. It relies on creating an Error object and getting it's stack trace.

class Foo {
    bar() {
        console.log(getMethodName());    
    }
}

function getMethodName() {
    var err = new Error();
    return /at \w+\.(\w+)/.exec(err.stack.split('\n')[2])[1] // we want the 2nd method in the call stack

}

new Foo().bar();

Not sure if this would help, but:

class Foo {
    bar() {
        console.log(Object.getOwnPropertyNames(Foo.prototype)); // ["constructor", "bar"]
    }
}

new Foo().bar();