473,670 Members | 2,623 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(ob j) {
if (typeof obj != "object" || obj === null) return false;
return /(\w+)\(/.exec(obj.const ructor.toString ())[1];
}

This approach supports also custom classes.

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

Thanks,

-- Pavils Jurjans
Jul 20 '05 #1
3 59240
Ivo
"Pavils Jurjans" <pa****@mailbox .riga.lv> wrote in message
news:95******** *************** ***@posting.goo gle.com...
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(ob j) {
if (typeof obj != "object" || obj === null) return false;
return /(\w+)\(/.exec(obj.const ructor.toString ())[1];
}

This approach supports also custom classes.

But, it seems pretty awkward to do Function.toStri ng 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(ob j) {
if (typeof obj != "object" || obj === null) return false;
return /(\w+)\(/.exec(obj.const ructor.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
"constructo r", 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.co nstructor == 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
"constructo r" 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.prototyp e 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.prototyp e, 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.toStri ng to get comperatively
simple information, a class name. Is there some alternatives?


ECMAScript supports some operators and object methods (inherited from
Object.prototyp e) 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
8566
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 class for each individual class. For example, ------------ import org.apache.log4j.Logger; public class Foo { ...
2
6907
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 a type mismatch. It is positively because of the boolean(java primitive)parameter. It goes fine if I change this parameter to int or String. This inteface has a lot more methods which works fine, it is just the
2
7727
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 cannot extract all of the type information from the wsdl using any standard tools. I'm using HttpGet style webservices if that makes any difference, but I want to be able to use any type. 1. I've tried the ServiceDescription.Types and I cannot...
0
3707
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) : base(message){} public FtpException(string message, Exception innerException) : base(message,innerException){}
9
1702
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
2251
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 I need to do some extra stuff in the click event of the created linkbutton. I tried to assign a delegate to the linkbutton's click event but I never gets in the procedure I assign. How can I trap the click event?
0
2909
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 action="/login"> <html:text property="userName" value="Bunty"/> <html:submit/>
2
6651
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) Location: class Week5MortgageGUI Week5MortgageLogic allint = logic.allInterest(amount, term, rate); Week5MortgageGUI.java:152:cannot find symbol symbol: method allInterest(double,double,double) Location: class Week5MortgageGUI
0
1544
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 databases in that server. Then after the user selects a database, I have another button that he/she click and I want to retrieve file groups from a specific table. At this point when he/she clicks on that button I get an error:
5
1628
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 MyFunctions Public Class BitHandling
0
8468
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8901
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8814
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8660
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7415
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6213
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5683
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4390
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1792
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.