Determining object type, finer-grained than typeof

J

James Marshall

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 (e-mail address removed) Berkeley, CA @}-'-,--
"Teach people what you know."
.............................................................................
 
D

Douglas Crockford

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/
 
J

James Marshall

Douglas Crockford said:
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
 
J

Jim Ley

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.
 

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,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top