473,395 Members | 2,151 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,395 software developers and data experts.

Determining object type, finer-grained than typeof

I need to detect the type of an object, more than just "object" as typeof
gives us. I'm writing a general handler that accepts a variety of objects
and properties, and acts accordingly depending on which type of object it
gets. Object types to be handled include windows, documents, locations,
links, images, layers, and so on. I'm using Mozilla, but I'm looking for
a platform-independent solution.

I know of four potential approaches to detect the object type, none of
which quite work: typeof, instanceof, the constructor property, and
toString() of the object. typeof doesn't give information beyond
"object". instanceof is the most promising, but I can't seem to get e.g.
"w instanceof Window" to work. For constructor, I can't find a match for
w.constructor where w is a window (e.g. "w.constructor==Window").
toString() is also promising, but doesn't work when toString() is
nonstandard, like for window.location, or when the user overrides
toString().

I can't be the first person to need this. Does anyone know a good
solution, or at least the best one available? Worst case, I could do a
heuristic solution that guesses the object's type based on which
properties exist, but that seems like a sloppy solution, and unreliable.

Thanks a lot for any tips!

James
.................................................. ...........................
James Marshall ja***@jmarshall.com Berkeley, CA @}-'-,--
"Teach people what you know."
.................................................. ...........................
Jul 20 '05 #1
3 12181
> I need to detect the type of an object, more than just "object" as typeof
gives us. I'm writing a general handler that accepts a variety of objects
and properties, and acts accordingly depending on which type of object it
gets. Object types to be handled include windows, documents, locations,
links, images, layers, and so on. I'm using Mozilla, but I'm looking for
a platform-independent solution.

I know of four potential approaches to detect the object type, none of
which quite work: typeof, instanceof, the constructor property, and
toString() of the object. typeof doesn't give information beyond
"object". instanceof is the most promising, but I can't seem to get e.g.
"w instanceof Window" to work. For constructor, I can't find a match for
w.constructor where w is a window (e.g. "w.constructor==Window").
toString() is also promising, but doesn't work when toString() is
nonstandard, like for window.location, or when the user overrides
toString().

I can't be the first person to need this. Does anyone know a good
solution, or at least the best one available? Worst case, I could do a
heuristic solution that guesses the object's type based on which
properties exist, but that seems like a sloppy solution, and unreliable.


In my opinion, in a properly designed type heirarchy, you don't need to know the
types of the individual objects. But I seem to be in the minority on that point.

If you must have type identification, make it explicit.

MyClass.prototype.type = "MyClass";

It is reliable and portable, at least for your objects. It also works across
contexts. DOM objects are another matter, although you can make things easier
for yourself with

window.type = "window";

and so on.

http://www.crockford.com/

Jul 20 '05 #2
"Douglas Crockford" <no****@laserlink.net> wrote in message news:<c0***************************@msgid.meganews servers.com>...
I need to detect the type of an object, more than just "object" as typeof
gives us. I'm writing a general handler that accepts a variety of objects
and properties, and acts accordingly depending on which type of object it
gets. Object types to be handled include windows, documents, locations,
links, images, layers, and so on. I'm using Mozilla, but I'm looking for
a platform-independent solution.

I know of four potential approaches to detect the object type, none of
which quite work: typeof, instanceof, the constructor property, and
toString() of the object. typeof doesn't give information beyond
"object". instanceof is the most promising, but I can't seem to get e.g.
"w instanceof Window" to work. For constructor, I can't find a match for
w.constructor where w is a window (e.g. "w.constructor==Window").
toString() is also promising, but doesn't work when toString() is
nonstandard, like for window.location, or when the user overrides
toString().

I can't be the first person to need this. Does anyone know a good
solution, or at least the best one available? Worst case, I could do a
heuristic solution that guesses the object's type based on which
properties exist, but that seems like a sloppy solution, and unreliable.


In my opinion, in a properly designed type heirarchy, you don't need to know the
types of the individual objects. But I seem to be in the minority on that point.

If you must have type identification, make it explicit.

MyClass.prototype.type = "MyClass";

It is reliable and portable, at least for your objects. It also works across
contexts. DOM objects are another matter, although you can make things easier
for yourself with

window.type = "window";

and so on.


Thanks, Douglas.

Unfortunately, I have little control over the objects in question;
they are part of others' Web pages. My project involves parsing all
JavaScript passing through a proxy, and trapping when something like
"location.href= newURL" is done, and passing such assignments to a
general handler. The objects I care about are virtually all DOM
objects (is Window part of the DOM?). This has to work with arbitrary
JavaScript, so I was hoping for a way to detect object type without
having to modify the objects. Also, I'm hoping for something
non-spoofable. Still, if nothing better comes along, your idea may be
workable in this situation.

If I use instanceof or toString(), are those results consistent enough
across browsers to be reliable? For example, does window.document
always appear as HTMLDocument, and are images always HTMLImageElement?
That's what they are in Mozilla.

Thanks,
James
Jul 20 '05 #3
On 6 Nov 2003 12:37:42 -0800, go***********@jmarshall.com (James
Marshall) wrote:
Unfortunately, I have little control over the objects in question;
they are part of others' Web pages. My project involves parsing all
JavaScript passing through a proxy, and trapping when something like
"location.href= newURL" is done, and passing such assignments to a
general handler.


Why not just trap location changes in your browser analysing script
seems to be a lot of trouble, when you could just modify the browser
to not let it happen.

Jim.
--
comp.lang.javascript FAQ - http://jibbering.com/faq/

Jul 20 '05 #4

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

Similar topics

3
by: Steve Brown | last post by:
Hello all, Is there a way to determine a variable's type at run-time? The reason I'm asking is that i have code that looks like this: template <class T> Object::Object(int TypeCode, T* data)...
2
by: Clinton Pierce | last post by:
I've filled a DataTable with columns that have custom type (a class that I'm using to keep track of other things, not just a value). When the .Select method goes to sort this column, how do I let...
0
by: CTDev Team | last post by:
Hi, We are using Exchange Server 5.5, and have applications written in VB6 and C# that read and process emails. We are experiencing intermittent errors similar to C# Application ...
7
by: Chuck Bowling | last post by:
Is there some way to determine what base class an instance of a object was derived from?
4
by: RTM | last post by:
Can anyone help me with the following issue? I've seen some similar questions here, but none relating to a textbox control.... I have a form with several controls, one of them being a textbox...
7
by: Martin Robins | last post by:
I am currently looking to be able to read information from Active Directory into a data warehouse using a C# solution. I have been able to access the active directory, and I have been able to return...
9
by: Clay_Culver | last post by:
I need to find out if an object is a class. Using new style classes this is very easy: class Test(object): pass obj = Test or obj = Test() if type(obj) == type:
53
by: Aaron Gray | last post by:
Due to M$'s stupidity in not making DOMElements first class citizens the following will not work :- function isElement( o) { return o instanceof Element } It works for FF, Opera and Safari.
11
by: Fuzzyman | last post by:
Hello all, I may well be being dumb (it has happened before), but I'm struggling to fix some code breakage with Python 2.6. I have some code that looks for the '__lt__' method on a class: ...
8
by: Fuzzyman | last post by:
Hello all, I may well be being dumb (it has happened before), but I'm struggling to fix some code breakage with Python 2.6. I have some code that looks for the '__lt__' method on a class: ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: 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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...

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.