473,414 Members | 1,674 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

Getting the class name as a string

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
3 59202
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
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
Thanks, Dom for your thorough view on this problem

Rgds,

-- Pavils
Jul 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Sibyl | last post by:
Is there any way to get the name of a class without an instance (i.e., object of the class)? I am working with log4j, and would like a uniform way to name loggers without typing in the name of the...
2
by: Eyal | last post by:
Hey, I would appriciate if anyone can help on this one: I have a java object/inteface having a method with a boolean parameter. As I'm trying to call this method from a javascript it fails on...
2
by: Danny Gagne | last post by:
I'm currently working an .net application (I can use 1.1 or 2.0 if needed) that needs to read a wsdl file and generate another piece of code that can use it. I'm encountering a problem where I...
0
by: ruju00 | last post by:
I am getting an error in Login() method of the following class FtpConnection public class FtpConnection { public class FtpException : Exception { public FtpException(string message) :...
9
by: Brian | last post by:
So I have this: double x = .25; double y = .5; double z = .25; double probability; int index; while(index < 30) {
6
by: Class | last post by:
Hi all, I create a gridview dynamicly because I don't know the columns in advance. I use the Templatefield to create a linkbutton. Everything fine..I have the postbackurl and it works. But now...
0
by: buntyindia | last post by:
Hi, I have a very strange problem with my application. I have developed it using Struts. I have a TextBox With Some fixed value in it and on Submit iam passing it to another page. <html:form...
2
by: karinmorena | last post by:
I'm having 4 errors, I'm very new at this and I would appreciate your input. The error I get is: Week5MortgageGUI.java:151:cannot find symbol symbol: method allInterest(double,double,double)...
0
by: TG | last post by:
Hi! Once again I have hit a brick wall here. I have a combobox in which the user types the server name and then clicks on button 'CONNECT' to populate the next combobox which contains all the...
5
by: tshad | last post by:
I have the following class in my VS 2008 project that has a namespace of MyFunctions. ********************************* Imports System Imports System.Text.RegularExpressions Namespace...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.