V
V
Vladimir B.2014-04-04 15:41:46
JavaScript
Vladimir B., 2014-04-04 15:41:46

How to find out the name of a "class" in JS when passing it to a function?

There is a "class":

Class = function( )
{
  // ***
};

Class.prototype.func = function( layer )
{
  // ***
};

I do inheritance:
boot.inherit = function( Child, Parent )
{
  // Наследуемся
};

boot.inherit( newClass, Class );

Question: How can I find out the name of the Parent (the variable to which the function is assigned) , without having to write it as a parameter?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Denis Pushkarev, 2014-04-04
@ange007

You are using an anonymous function, so no way.
If you have a named function, it has a name property . It is not supported by IE, if you need its support - you can tear out the name from the source of the function.

function Class(){}
Class.name // => 'Class'
function getName(it){
  var match = /^\s*function\s+(\b\w+\b)/.exec(it);
  if(match)return match[1];
}
getName(Class) // => 'Class''

However, this is less than completely useless - after compressing your code, the name of the constructor and, accordingly, the value of the name property will turn from 'Class' to some 'tF'.
If you need to get exactly the name of the function - add to the function and use the displayName property - in one go and improve code profiling .
function Class(){}
Class.displayName = 'Class';

M
maxaon, 2014-04-04
@maxaon

Google it!
webreflection.blogspot.com/2007/01/javascript-getc...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question