By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,454 Members | 1,804 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,454 IT Pros & Developers. It's quick & easy.

Getting the class name as a string

P: n/a
Hallo,

Is there some decent way how to get the object class name in a string
format?

Currently I use this:

function getClassName(obj) {
if (typeof obj != "object" || obj === null) return false;
return /(\w+)\(/.exec(obj.constructor.toString())[1];
}

This approach supports also custom classes.

But, it seems pretty awkward to do Function.toString to get comperatively
simple information, a class name. Is there some alternatives?

Thanks,

-- Pavils Jurjans
Jul 20 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Ivo
"Pavils Jurjans" <pa****@mailbox.riga.lv> wrote in message
news:95**************************@posting.google.c om...
Hallo,

Is there some decent way how to get the object class name in a string
format?
What sort of object? A HTML element? How 's this?
<a class="hello" onclick="alert(this.className)">Click me</a>
<a class="hello" onclick="alert(typeof this.className)">Me too</a>

Currently I use this:

function getClassName(obj) {
if (typeof obj != "object" || obj === null) return false;
return /(\w+)\(/.exec(obj.constructor.toString())[1];
}

This approach supports also custom classes.

But, it seems pretty awkward to do Function.toString to get comperatively
simple information, a class name. Is there some alternatives?

Thanks,

-- Pavils Jurjans

Jul 20 '05 #2

P: n/a
Pavils Jurjans wrote:
Hallo,

Is there some decent way how to get the object class name in a string
format?
<rant>

For native javascript objects (that you construct) label the objects
with a "className" property string value that you supply or take some
alternative but otherwise custom approach.

Seriously, because javascript has no concept of "class name" and
currently only supports linking objects by means of a prototype chain.
Currently I use this:

function getClassName(obj) {
if (typeof obj != "object" || obj === null) return false;
return /(\w+)\(/.exec(obj.constructor.toString())[1];
}

This approach supports also custom classes.
Yes, but only for minimalistic prototyping chains.

When a function object, say F, is created, a hidden property named
"constructor", with value F, is generated by the system in a prototype
object created and used to initialise the ".prototype" property of F.

So following creation of function object F,

(F.prototype.constructor == F)

holds true.

If an object is constructed from function F, the internal prototype
chain pointer of the constructed object is statically set to the value
of F.prototype at time of construction, from where a hidden
"constructor" property is inherited. So without alteration to the
prototype chain:

function F(){};
var f = new F();
f.constructor == F; // inherited

holds true. If the initial value of F.prototype is overwritten, however,
as in

function F(){};
function G(){};
F.prototype = new G();
f = new F();

then f inherits its constructor property value from F.prototype, which
in turn inherits it from G.prototype, from where its value will be
returned as G. So now (f.constructor == G) holds true.

Consider also that a function's prototype property could be set to an
object expression (showing Object as its constructor) or a preexisting
object of any kind, including Object.prototype for that matter. Agreed
you won't see examples of this in documentation prepared for class
oriented programmers if Netscape documentation is any guide to go by.

Consider also that system supplied prototype objects, created in tandem
with function objects, have their internal prototype chain (object
value) set to Object.prototype, which lists Object as its constructor.

Hence ECMAScript does *not* provide a means of enquiring the constructor
function called to create an object. It does guarantee that the
constructor property of objects will refer to a function which
constructed the first and/or second object in a prototype chain starting
from the Object.protoype end.
</rant>

But, it seems pretty awkward to do Function.toString to get comperatively
simple information, a class name. Is there some alternatives?


ECMAScript supports some operators and object methods (inherited from
Object.prototype) related to prototype chains. These include the "in"
and "instanceof" binary operators, and hasOwnProperty(), hasProperty(),
isPrototypeOf() object methods. Support for any and all of these is
browser and version dependent, as is appplicability to host or DOM node
objects. Usefulness of the instanceof operator may assume the near
universal case that function prototype property values are set for the
life of a program.
HTH,

Dom

Jul 20 '05 #3

P: n/a
Thanks, Dom for your thorough view on this problem

Rgds,

-- Pavils
Jul 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.