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."
.............................................................................
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."
.............................................................................