Re: Get object name?

Giganews Newsgroups
Subject: Re: Get object name?
Posted by:  Lasse Reichstein Nielsen (l…@hotpop.com)
Date: Mon, 17 Mar 2008

howa <howach…@gmail.com> writes:

> A simple code like:
>
> a = new User();
>
> How can I print out the class name which a belong to?

If you mean the name of the constructor function (Javascript has no
classes) then by default you can find the constructor method as:
var cons = a.constructor;
and you can extract the name from its string representation:

function functionName(func) {
var match = /function\s+(\w+)\b/.exec(func.toString());
if (match) {
  return match[1];
} else {
  return "[anonymous]";
}
}

Now, the big question is: What do you need this for?

Good luck
/L

--
Lasse Reichstein Nielsen  -  l…@hotpop.com
DHTML Death Colors: <URLhttp://www.infimum.dk/HTML/rasterTriangleDOM.html>
  'Faith without judgement merely degrades the spirit divine.'

Replies

In response to

Get object name? posted by howa on Mon, 17 Mar 2008