Bizarre instanceof behaviour

Discussion in 'Javascript' started by Sean Inglis, Jan 11, 2007.

  1. Sean Inglis

    Sean Inglis Guest

    Well bizarre to me, anyway.

    I've distilled it down to two small files:


    testtop.htm
    ===============================
    <html>
    <head>
    <script language="Javascript">

    var TOPTEST = [1,2,3];

    alert('hellotop');
    alert(TOPTEST instanceof Array);
    </script>
    </head>
    <frameset rows="100%">
    <frame src="testchild.htm">
    </frameset>
    </html>
    ===============================


    and testchild.htm
    ===============================
    <html>
    <head>

    <script language="Javascript">

    function testtop() {
    alert('hellochild');
    alert(parent.TOPTEST);
    alert((parent.TOPTEST) instanceof Array);
    }

    </script>
    </head>
    <body>
    <h1>body</h1>
    <input type="BUTTON" onclick="testtop();" value="test">
    </body>
    </html>
    ===============================


    A few deprecated features aside, when I load testtop.htm, instanceof
    array is true as I'd expect, but when I check this from the button in
    testchild.htm, instanceof array is false.

    I suspect I'm missing something obvious, as this behaviour is
    consistent in IE 6.0, Opera 9.1 and Firefox 2.0.0.1 (all WIN32, I'll
    check Linux when I get home).

    Any pointers to what I'm missing, or can anyone shed any light on this
    behaviour?

    Thanks

    Sean
    Sean Inglis, Jan 11, 2007
    #1
    1. Advertising

  2. Sean Inglis

    Matt Kruse Guest

    Sean Inglis wrote:
    > A few deprecated features aside, when I load testtop.htm, instanceof
    > array is true as I'd expect, but when I check this from the button in
    > testchild.htm, instanceof array is false.


    When the frameset document resolves "Array" it finds it defined in its
    window object: window.Array.
    The main document has its own window object, and its own version of
    window.Array.

    So, the main documents Array object isn't the same object as the frameset's
    Array object, even though they are both instances of the same native object.

    Instead of this:
    > alert((parent.TOPTEST) instanceof Array);


    try this:
    alert((parent.TOPTEST) instanceof parent.Array);

    Or, if you just care that the object looks and acts like an Array, try
    something like:

    /**
    * Return true if an object is not undefined
    */
    function def(o) {
    return typeof(o)!="undefined";
    }
    /**
    * Try to figure out if an object can be treated like an Array - ie,
    iterated over using numeric indexes
    */
    function isArrayLike(o) {
    if (o==null || typeof(o)!="object" || typeof(o.length)!="number") {
    return false;
    }
    // Check to see if the object is an instance of the window's Array object
    if (def(Array) && def(o.constructor) && o.constructor==Array) {
    return true;
    }
    // It might be an array defined from another window object - check to see
    if it has an Array's methods
    if (typeof(o.join)=="function" && typeof(o.sort)=="function" &&
    typeof(o.reverse)=="function") {
    return true;
    }
    // As a last resort, let's see if index [0] is defined
    return (o.length==0 || def(o[0]));
    };

    --
    Matt Kruse
    http://www.JavascriptToolbox.com
    http://www.AjaxToolbox.com
    Matt Kruse, Jan 11, 2007
    #2
    1. Advertising

  3. Sean Inglis

    Sean Inglis Guest

    Matt Kruse wrote:
    > Sean Inglis wrote:
    > > A few deprecated features aside, when I load testtop.htm, instanceof
    > > array is true as I'd expect, but when I check this from the button in
    > > testchild.htm, instanceof array is false.

    >
    > When the frameset document resolves "Array" it finds it defined in its
    > window object: window.Array.
    > The main document has its own window object, and its own version of
    > window.Array.
    >
    > So, the main documents Array object isn't the same object as the frameset's
    > Array object, even though they are both instances of the same native object.
    >
    > Instead of this:
    > > alert((parent.TOPTEST) instanceof Array);

    >
    > try this:
    > alert((parent.TOPTEST) instanceof parent.Array);
    >
    > Or, if you just care that the object looks and acts like an Array, try
    > something like:
    >
    > /**
    > * Return true if an object is not undefined
    > */
    > function def(o) {
    > return typeof(o)!="undefined";
    > }
    > /**
    > * Try to figure out if an object can be treated like an Array - ie,
    > iterated over using numeric indexes
    > */
    > function isArrayLike(o) {
    > if (o==null || typeof(o)!="object" || typeof(o.length)!="number") {
    > return false;
    > }
    > // Check to see if the object is an instance of the window's Array object
    > if (def(Array) && def(o.constructor) && o.constructor==Array) {
    > return true;
    > }
    > // It might be an array defined from another window object - check to see
    > if it has an Array's methods
    > if (typeof(o.join)=="function" && typeof(o.sort)=="function" &&
    > typeof(o.reverse)=="function") {
    > return true;
    > }
    > // As a last resort, let's see if index [0] is defined
    > return (o.length==0 || def(o[0]));
    > };
    >
    > --
    > Matt Kruse
    > http://www.JavascriptToolbox.com
    > http://www.AjaxToolbox.com


    Thanks Matt, annoying then, and counter-intuitive (AFAIC), but not
    quite the show-stopper I thought it was.

    This actually manifests itself in the version of json.js I'm using,
    rather than in any of "my" code. I'll check the latest version and
    report it as a problem if needed.

    Sean
    Sean Inglis, Jan 12, 2007
    #3
  4. > This actually manifests itself in the version of json.js I'm using,
    > rather than in any of "my" code. I'll check the latest version and
    > report it as a problem if needed.


    Load the latest json.js into both frames.

    http://www.JSON.org/
    Douglas Crockford, Jan 12, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Pascal Herczog

    This is bizarre html behaviour...

    Pascal Herczog, Nov 13, 2005, in forum: HTML
    Replies:
    5
    Views:
    465
    Toby Inkster
    Nov 15, 2005
  2. Simon Pryor
    Replies:
    5
    Views:
    1,113
    Peter Kragh
    Jun 17, 2004
  3. Replies:
    21
    Views:
    21,317
  4. =?iso-8859-1?B?THXtcyBBbW9yaW0=?=

    instanceof strange behaviour

    =?iso-8859-1?B?THXtcyBBbW9yaW0=?=, Sep 15, 2006, in forum: Java
    Replies:
    5
    Views:
    369
    Andrea Desole
    Sep 19, 2006
  5. musosdev
    Replies:
    0
    Views:
    562
    musosdev
    Jul 1, 2008
Loading...

Share This Page