Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
| 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 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 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
 
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.
 
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

 
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
 
Old July 31st, 2007, 04: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




Similar Threads
Thread Thread Starter Forum Replies Last Post
Object Properties & document.activeElement interrupt Javascript How-To 4 April 21st, 2005 03: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 04:35 PM
How to set (right click)properties for an object mpkrishna Classic ASP Components 1 August 24th, 2003 01:30 AM





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