473,395 Members | 1,745 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Bizarre instanceof behaviour

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

Jan 11 '07 #1
4 1892
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
Jan 11 '07 #2

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

Jan 12 '07 #3
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/
Jan 12 '07 #4
On Fri, 12 Jan 2007 14:54:45 +0000, Douglas Crockford wrote:
>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/
Yes, I checked the latest version of the source and realised that it
wouldn't suffer the same limitation, so I didn't report it as a problems.

Unfortunately the new version is sufficiently different in approach that
I'd be unable to make the changes required to our code in a timely way
(this time round anyway), so I've adopted some of the workarounds
suggested by Matt.
Jan 12 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: John MacIntyre | last post by:
Hi, I have a page with a series of child pages loaded into an iframe. When I move from page to page, I store an object containing the child's control data in a variable on the main page, then...
7
by: chirs | last post by:
Hi, These 2 lines caused an error in IE5. The error is "Function expected". Why? var d=new Date(); document.write(d instanceof Object + "<br>"); Thanks.
4
by: The Plankmeister | last post by:
This really is very strange... Have a look at this: http://www.plankmeister.org.uk/new/index.html in IE6. When it loads, the top section is blank. If you mouseover anything in the menu half of...
5
by: Beginner | last post by:
What is the c++ version of instanceof()? Thank-you
14
by: Nick Maclaren | last post by:
x = (1.234567890125, 1.2345678901255) print x print x, x Is there a rational reason, or is that simply an artifact of the way that the code has evolved? It is clearly not a bug :-) ...
2
by: System Administrator | last post by:
function a() { } typeof a //returns 'function' a instanceof a //returns false typeof Object //returns 'function' Object instanceof Object //returns ...
30
by: kj | last post by:
My book (Flanagan's JavaScript: The Definitive Guide, 5th ed.) implies on page 111 that the following two constructs are equivalent: ( x.constructor == Foo ) and ( x instanceof Foo ) The...
3
by: whitelined | last post by:
Hi, How widely supported is the instanceof operator? Is there an alternative to seeing if an object is an instance of a constructor? Many thanks Regards Aaron
0
by: =?Utf-8?B?bXVzb3NkZXY=?= | last post by:
Hi guys I'm using a URLRewrite module to redirect certain URLs in my .net 2.0 website. However I am getting som really bizarre behaviour public void UrlRewriter_BeginRequest(object sender,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.