Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old September 27th, 2007, 12:01 PM
Friend of Wrox
 
Join Date: Oct 2004
Location: Clinton, UT, USA.
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
Reply With Quote
  #2 (permalink)  
Old September 27th, 2007, 01:10 PM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
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)
Reply With Quote
  #3 (permalink)  
Old September 28th, 2007, 09:28 AM
Friend of Wrox
 
Join Date: Oct 2004
Location: Clinton, UT, USA.
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
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 02:15 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.