Wrox Programmer Forums
|
BOOK: Professional JavaScript for Web Developers ISBN: 978-0-7645-7908-0
This is the forum to discuss the Wrox book Professional JavaScript for Web Developers by Nicholas C. Zakas; ISBN: 9780764579080
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional JavaScript for Web Developers ISBN: 978-0-7645-7908-0 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 9th, 2006, 03:52 AM
Authorized User
 
Join Date: Mar 2006
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default Object no longer instanceof object

I ran into this problem I can't figure out. I have an object (oCar) that I set as a property of another object (oPerson). When I pass oPerson as an argument, the property containing oCar, will no longer be an instanceof Object:
Code:
var oCar = new Object();
oCar.model = "Volvo";
oCar.color = "red";

var oPerson = new Object();
oPerson.car = oCar;

alert(oPerson.car instanceof Object); // true

test(oPerson.car);

function test(oData)
{
    alert(oData.car instanceof Object); // false
}
__________________
José Jeria
 
Old April 9th, 2006, 04:50 AM
AGS AGS is offline
Wrox Technical Editor
 
Join Date: May 2005
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I believe there is just a misprint in your code.
You are passing oPerson.car as an argument to test() function and further is trying to get car property from the instance of the car object itself. The value of car.car is "undefined" and instanceof Object returns "false".

Best regards,
Alexei

 
Old April 9th, 2006, 05:08 AM
Authorized User
 
Join Date: Mar 2006
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default

It's not undefined, try this:
Code:
function test(oData)
{
    alert(oData.car instanceof Object); // false
    alert(oData.model); // Volvo
    alert(oData.color); // red
}
José Jeria
 
Old April 9th, 2006, 05:21 AM
AGS AGS is offline
Wrox Technical Editor
 
Join Date: May 2005
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Frankly speaking I don't understand the point.
The oPerson object has a car property. You are passing THIS property as an argument to the test() function.
Quote:
quote:test(oPerson.car);
And then you are trying to get car property from the oCar instance but it doesn't have one. But it does have of course model and color properties.
Try this
Code:
function test(oData)
{
    alert(typeof oData.car); 
    alert(oData.model); // Volvo
    alert(oData.color); // red
}
Alexei

 
Old April 9th, 2006, 12:19 PM
nzakas's Avatar
Wrox Author
 
Join Date: Dec 2004
Posts: 217
Thanks: 0
Thanked 5 Times in 5 Posts
Default

Jose,

The point Alexei is making is that you are passing in the car object to the function but you are treating it as if you are passing in the person object. So, when you try oData.car instanceof Object, it returns false since there is no oPerson.car.car, meaning that this property is undefined and therefore not an instance of an Object.

Nicholas C. Zakas
Author, Professional JavaScript for Web Developers (ISBN 0764579088)
http://www.nczonline.net/
 
Old May 12th, 2006, 06:22 AM
Authorized User
 
Join Date: Mar 2006
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello guys,

sorry for have wasted your time. My testcase that I provided was not really showing the issue. It very obvious why the code I provided above is wrong. Doh.

Anyway, I now have an example that shows the problem I was having. Basically an object is no longer instanceof Object when it is passed to another window.

Main window html:
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript">
window.onload = function()
{
    window.open("popup.html", "", "width=200, height=200");
}

function checkIfObject(oTest)
{
    var sMsg = "Object passed to main window. Instance of Object == ";
    var bIsObject = oTest instanceof Object;
    alert(sMsg + bIsObject); // this will alert false
}
</script>
</head>

<body>


</body>
</html>
Popup window html:
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Popup</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript">
function checkIfObject(oTest)
{
    var sMsg = "Car object created. Instance of Object == ";
    var bIsObject = oTest instanceof Object;
    alert(sMsg + bIsObject); // this will alert true
}

var oCar = new Object();
oCar.color = "red";
oCar.amountOfDoors = 4;

checkIfObject(oCar);
opener.checkIfObject(oCar);
</script>
</head>

<body>


</body>
</html>
Due to this, we had to use the JSON.stringify(oMyObj) method (see json.org) to pass object between windows... Would be interesting to know why this doesn't really work.

What is interesting is that I can access the properties of the object. Odd.

Regards,

/José Jeria
 
Old May 12th, 2006, 07:47 AM
AGS AGS is offline
Wrox Technical Editor
 
Join Date: May 2005
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Default

José,

There is an explanation:
http://www.thescripts.com/forum/thread91190.html

Best regards,
Alexei



 
Old May 12th, 2006, 08:22 AM
Authorized User
 
Join Date: Mar 2006
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Alexei, that explains the issue I am having pretty well.
 
Old May 12th, 2006, 08:32 AM
Authorized User
 
Join Date: Mar 2006
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default

So the 2 solutions for this would be:

a) Do what I am doing now, to serialize the object to a string and then pass it
b) Have a global variable in the main window that gets set by the child window

Any thoughts on what would be the best solution for this? Option b pollutes the window object, but solution a sounds to be a bit of overkill, but no need for global variables (we are developing in a very strict oo enviroment)

Regards,

/José Jeria





Similar Threads
Thread Thread Starter Forum Replies Last Post
Serializing Object Graph - Assigning Object to Jag venkat.kl C# 0 August 28th, 2006 10:39 AM
Object Reference not set to an instance of object srinivas_chakka ASP.NET 1.0 and 1.1 Professional 0 February 8th, 2006 11:56 AM
create a Line object ,Box object in CR at Runtime? thanhnt Pro VB 6 1 May 16th, 2005 06:51 AM
Error Occurred creating Report Object: Object does sa_moizatyahoo Classic ASP Professional 0 February 1st, 2005 10:29 AM
Error: Object is no longer valid. Daniel Schaffer Classic ASP Databases 1 March 28th, 2004 11:30 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.