Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript
|
Javascript General Javascript discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript 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 September 27th, 2007, 12:01 PM
Friend of Wrox
 
Join Date: Oct 2004
Posts: 564
Thanks: 0
Thanked 4 Times in 4 Posts
Default Passing / Referencing Objects

This could be a tricky one, but I am sure it can be done - Just not sure how.

I have a global object which I declare at the start of my script.

In one of my functions, I then want to set that object based on a function which I am including from a library.

The library function initiates an AJAX call, and then attempts to copy the returning xmlObject to the Global Object. That all works fine, except the Global object doesn't hold the xmlObject once it leaves the function. I suspect that a local object is being created when I pass it in.

I think the easy question is this... Is there a way to reference the Global variable without just calling it's name? Something like window.Objects("GlobalObject")

If not, here is what I am trying to accomplish

globalObject = new Object();

assignObject(globalObject, 1, 2);

//Heres the library function (needs to be very generic)
assignObject(globalObjectName, param1, param2)
{
     'Logic here to retrieve an xmlObject
     globalObjectName = xmlObject;
}

I then want to work with the assignObject to retrieve the XML.

Any ideas? Clarification needed?

Thanks

Mike

Mike
EchoVue.com
__________________
Mike
EchoVue.com
 
Old September 27th, 2007, 01:10 PM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

You'll have to show some simplified code. If you do this:
Code:
var o;

function doSomething()
{
  o = {name: "Joe"};
}

doSomething();
alert(o.name);
Then the o object declared at the beginning will have a property "name" with the value "Joe". If you use var inside the function then you are creating a different local variable.

Addition:
Sorry, didn't read your code carefully enough. JavaScript does not support passing by reference, you have two choices. Either just refer to the global object in the function or you assign the properties from one object to the other:
Code:
var o = {name: "Joe"};

function changeObjectName(obj, name)
{
  obj.name = name;
}

function changeObjectName2(name)
{
  o.name = name;
}

alert(o.name);
changeObjectName(o, "Joseph");
alert(o.name);
changeObjectName2("Joe Fawcett");
alert(o.name);
--

Joe (Microsoft MVP - XML)
 
Old September 28th, 2007, 09:28 AM
Friend of Wrox
 
Join Date: Oct 2004
Posts: 564
Thanks: 0
Thanked 4 Times in 4 Posts
Default

Thanks Joe,

That was the sad realization I came to. Found another way to pass the data, since the boss didn't want global variables floating around either! I was using a Javascript array, but since all I need is a list of text/value pairs, I wnet with a hidden select box instead. Works just as well, and actually makes debugging a fair bit easier.

Thanks again,

Mike

Mike
EchoVue.com





Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing objects between One Page to next page muralidharan.d ASP.NET 1.0 and 1.1 Basics 2 July 3rd, 2007 07:48 PM
passing jagged array of objects nutrino Beginning VB 6 0 January 27th, 2006 11:58 AM
Passing Objects by value Ibn_Aziz C# 7 December 30th, 2003 06:07 AM
Passing Objects nayan_k Classic ASP Basics 1 September 16th, 2003 01:58 AM





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