17

I am trying to retrieve the class name from within a static method. It works from an ordinary method but not from within a static method

 class MyNode{
    constructor(){
        var classname=this.constructor.toString().split ('(' || /s+/)[0].split (' ' || /s+/)[1];
        console.log(classname);
    }
    static a_static_method(){
        var classname=this.constructor.toString().split ('(' || /s+/)[0].split (' ' || /s+/)[1];
        console.log(classname);
    }
}
var obj=new MyNode(); // THIS WORKS, prints "MyNode" 
MyNode.a_static_method(); // THIS DOESN'T, prints "Function"

I forgot to tell: it should work for the derived classes of MyNode.

1
  • FYI: in the non static method, you can use this.constructor.name instead of that long regexp Commented Aug 7, 2017 at 16:06

4 Answers 4

30

Now you can just use this.name

Sign up to request clarification or add additional context in comments.

2 Comments

It feels so strange referencing this from a static context, but it works.
The official TS 1.8 documentation states (8.2.1): "Within static member functions and static member accessors, the type of this is the constructor function type."
7

Please check following solution:

class MyNode{
    constructor(){
        var classname=this.constructor.toString().split ('(' || /s+/)[0].split (' ' || /s+/)[1];
        console.log(classname);
    }
    static a_static_method(){
        var classname = this.toString().split ('(' || /s+/)[0].split (' ' || /s+/)[1];
        console.log(classname);
    }
}

In derived class you will get the name of that class, not MyNode

1 Comment

This answer is quite old, please see the answer below. You can use this.name now, apparently.
4

Previous answers will fail in certain cases

class cl_s2{
	constructor(){
    }

static a_static_method(){
        var classname = this.toString().split ('(' || /s+/)[0].split (' ' || /s+/)[1];
        console.log(classname);
    }

static get is() {
    return this.toString().replace(new
    RegExp('^class(?: |\n|\r)+([^\s\n\r{]+)[\s\n\r{](?:\s|\n|\r|.)+', 'i'), '$1').trim();
  }

static get className() {
        let classNameRegEx = /(?:\S+\s+){1}([a-zA-Z_$][0-9a-zA-Z_$]*)/;
        return classNameRegEx.exec( this.toString() )[1];
    }
}

//Will fail in chrome because toString will return " class cl_s2{ ...", so there will be no "("
cl_s2.a_static_method();

//Will fail in browser which will display "function cl_s2(){" instead
// and in classes with underscore in name
console.log(cl_s2.is);

//this will work as long as you do not use unicode chars in classnames
console.log(cl_s2.className);

Comments

0

Improved method.

How to get class name in a static method

Useful for polymer 2.0 elements

The following code :

class G {
  static get is() {
    return this.toString().replace(new
    RegExp('^class(?: |\n|\r)+([^\s\n\r{]+)[\s\n\r{](?:\s|\n|\r|.)+', 'i'), '$1').trim();
  }
}; 

G.is

Gives you :

"G"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.