Getting the name of a class constructed by an anonymous function Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Getting the name of a class constructed by an anonymous function without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I need to work inside a namespace, say I make this

var my_namespace = {}

my_namespace.my_class = function() {}

var instance = new my_namespace.my_class();

Then the current problem I’m facing is that instance.constructor.name returns “” (an empty string).

I want to check the type of instance by comparing it against an array of possibilities such as ‘String’ or ‘Array’ or ‘my_namespace.my_class’

Theoretically I could work around it by using instanceof and eval but I’m looking for a less volatile way.

Answer

Have you considered not using an anonymous function? Something like:

var my_namespace = {};
my_namespace.my_class = function MyClass() {};
var instance = new my_namespace.my_class();

console.log(instance.constructor.name);
We are here to answer your question about Getting the name of a class constructed by an anonymous function - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji