By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,575 Members | 1,413 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,575 IT Pros & Developers. It's quick & easy.

Determining object type, finer-grained than typeof

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
> 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

P: n/a
"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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.