Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Bizarre instanceof behaviour

Reply
Thread Tools

Bizarre instanceof behaviour

 
 
Sean Inglis
Guest
Posts: n/a
 
      01-11-2007
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

 
Reply With Quote
 
 
 
 
Matt Kruse
Guest
Posts: n/a
 
      01-11-2007
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


 
Reply With Quote
 
 
 
 
Sean Inglis
Guest
Posts: n/a
 
      01-12-2007

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

 
Reply With Quote
 
Douglas Crockford
Guest
Posts: n/a
 
      01-12-2007
> 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/
 
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
instanceof strange behaviour =?iso-8859-1?B?THXtcyBBbW9yaW0=?= Java 5 09-19-2006 08:06 AM
instanceof NOT (always) bad? The instanceof myth. dmx_dawg@hotmail.com Java 21 07-20-2006 07:06 PM
This is bizarre html behaviour... Pascal Herczog HTML 5 11-15-2005 10:30 PM
std::ostringstream bizarre behaviour. Please help! Simon Pryor C++ 5 06-17-2004 10:48 PM
AVG update - bizarre behaviour Trent C Computer Support 1 03-07-2004 12:21 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57