Re: Get object name?

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

howa <howach…> 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

Lasse Reichstein Nielsen  -  l…
DHTML Death Colors: <URL>
  'Faith without judgement merely degrades the spirit divine.'


In response to

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