Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Determining object type, finer-grained than typeof

Reply
Thread Tools

Determining object type, finer-grained than typeof

 
 
James Marshall
Guest
Posts: n/a
 
      11-06-2003
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 http://www.velocityreviews.com/forums/(E-Mail Removed) Berkeley, CA @}-'-,--
"Teach people what you know."
.................................................. ...........................
 
Reply With Quote
 
 
 
 
Douglas Crockford
Guest
Posts: n/a
 
      11-06-2003
> 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/

 
Reply With Quote
 
 
 
 
James Marshall
Guest
Posts: n/a
 
      11-06-2003
"Douglas Crockford" <(E-Mail Removed)> wrote in message news:<c00f7$3faa6650$4365624c$(E-Mail Removed) 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
 
Reply With Quote
 
Jim Ley
Guest
Posts: n/a
 
      11-06-2003
On 6 Nov 2003 12:37:42 -0800, (E-Mail Removed) (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/

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
typeof and g++ fabioppp C++ 10 02-24-2005 09:48 AM
Using TypeOf? Leon ASP .Net 7 11-01-2004 11:41 PM
finding the type of an object? ("typeof" doesn't work) Bennett Haselton Javascript 7 05-20-2004 05:47 PM
Use of typeof() for member data declaration (in g++) M. Steinbach C++ 1 02-20-2004 10:28 AM
Use of typeof() for member data declaration (g++ bug?) M. Steinbach C++ 0 02-20-2004 08:40 AM



Advertisments