Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To 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 Display Modes
  #1 (permalink)  
Old February 7th, 2007, 03:39 AM
Authorized User
 
Join Date: May 2006
Location: , , .
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to kaos_frack
Default how to access parent object properties

I have nested objects and a child object needs to access parent object's properties and/or methods. How do I accomplish that?
I've seen somewhere '__parent__' property of objects that point to the parent object, but it's not working?

Thanks
Reply With Quote
  #2 (permalink)  
Old February 11th, 2007, 09:00 PM
Friend of Wrox
 
Join Date: Nov 2005
Location: Rotterdam, , Netherlands.
Posts: 223
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

Are you talking about javascript DOM?
What exactly are you trying to access you can access and manipulate html for example
Code:
<script>
function changeDivContent()
{
document.getById['myDiv'].innerHTML="new text";
}
</script>
<div id="myDiv">
   this you can change
</div>
<button onClick="changeDivContent()"></button>
with this code you will change the text inside the div

Is this what you are trying to do? methods you cant acces with javascript.

Post your code to show what you mean.
__________________________________________________ ________
I am DJ Kat...that's my name. Its a D and a J and a Kat with a K.
Reply With Quote
  #3 (permalink)  
Old February 12th, 2007, 02:27 AM
Authorized User
 
Join Date: May 2006
Location: , , .
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to kaos_frack
Default

Hi Dj Kat,

I'm not about DOM elements
I have create user defined objects:
Code:
var Garage = {
   location: "2920 6th Ave",
   car1: {
      color: "red",
      sayColor: function() {
         alert(this.color);
      },
      getGarage: function() {
         return [parent].name;
      }
   }
};
In the above code how can car1 access Garage's location property?
__parent___ property does not work on IE

Reply With Quote
  #4 (permalink)  
Old February 17th, 2007, 09:52 AM
Authorized User
 
Join Date: May 2006
Location: , , .
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to kaos_frack
Default

I've found solution! Here's the code:
Code:
Object.prototype.addChild = function(property, child) {
   child._parent = this;
   this[property] = child;
   return;
};
You shoud enter the above code before any other code, then you should add a child object in this way:
Code:
garage.addChild("mycar", new car())
// from now on 'mycar' object inside 'garage' object will have a property
// '_parent' that points to the 'garage' object
Reply With Quote
  #5 (permalink)  
Old July 31st, 2007, 05:31 AM
Registered User
 
Join Date: Jul 2007
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the code kaos_frack. Below is a simple modification that allows the parent's child object be an array of objects, with each object in the array having access to the parent:

Object.prototype.addChildArray = function(property, childArray) {
   for(var i=0;i<childArray.length;++i){
     childArray[i]._parent = this;
   }
   this[property] = childArray;
   return;
};

Dan@FP
Reply With Quote
Reply


Thread Tools
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
Object Properties & document.activeElement interrupt Javascript How-To 4 April 21st, 2005 04:49 PM
Extracting COM Object Properties walkins5 General .NET 0 February 5th, 2005 09:34 PM
Properties/Methods Not in Object Browser ritag VS.NET 2002/2003 0 January 13th, 2005 12:59 PM
Object properties in an ArrayList augustwest C# 4 June 16th, 2004 05:35 PM
How to set (right click)properties for an object mpkrishna Classic ASP Components 1 August 24th, 2003 02:30 AM



All times are GMT -4. The time now is 11:47 PM.


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