Determining object type, finer-grained than typeof

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • James Marshall

    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 james@jmarshall .com Berkeley, CA @}-'-,--
    "Teach people what you know."
    ............... ............... ............... ............... ............... ..
  • Douglas Crockford

    #2
    Re: Determining object type, finer-grained than typeof

    > I need to detect the type of an object, more than just "object" as typeof[color=blue]
    > 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.[/color]

    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.prototy pe.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.



    Comment

    • James Marshall

      #3
      Re: Determining object type, finer-grained than typeof

      "Douglas Crockford" <nospam@laserli nk.net> wrote in message news:<c00f7$3fa a6650$4365624c$ 31742@msgid.meg anewsservers.co m>...[color=blue][color=green]
      > > 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.[/color]
      >
      > 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.prototy pe.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.[/color]

      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.h ref= 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 HTMLImageElemen t?
      That's what they are in Mozilla.

      Thanks,
      James

      Comment

      • Jim Ley

        #4
        Re: Determining object type, finer-grained than typeof

        On 6 Nov 2003 12:37:42 -0800, google_signup@j marshall.com (James
        Marshall) wrote:
        [color=blue]
        >Unfortunatel y, 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.[/color]

        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.javas cript FAQ - http://jibbering.com/faq/

        Comment

        Working...