Getting the class name as a string

P

Pavils Jurjans

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
 
I

Ivo

Pavils Jurjans said:
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?
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
 
D

Dom Leonard

Pavils said:
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.


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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,285
Latest member
CryptoTaxxSoftware

Latest Threads

Top