2

I would like to know if it's possible from an object to access the properties of its parent, here is an explicite example, let's say we have an array of People objects in a Group object.

In this Group object, every People have the same address, so it will be nice to declare it in the Group and not in each People objects, but how to access it without parsing the collection?

function Group() {
  this.address = 'EveryWhere';
  this.Collection = [];
} 

function People(data) {
  this.name = data.name;

  this.getAddress = function() {
    return this.address; //how to access it (declared in the Group object)?
  }
}

var Serie = new Group();
var John = new People();
Serie.Collection.push(John);
console.log(John.getAddress());
2
  • Pass parent reference new People( data , Serie ) .. and in function People( data , serieRef) { this.parent = SerieRef
    – rab
    Jul 9, 2013 at 9:18
  • 1
    A couple of style points: 1. By convention, initial capitals are used in JavaScript for constructor functions, so function Group() is good, but this.Collection = [];, var Serie and var John are odd because they're not constructor functions. 2. Person might be a better choice than People for that constructor function, as it looks like you use it to create one person, not some people. Jul 9, 2013 at 9:37

2 Answers 2

2

Same as in many languages: pass the parent into the constructor of the child so you can hold a reference to it.

function People(data, parent) {
    this.parent = parent;
    this.getAddress = function() {
        return this.parent.address;
    }
}

To make it safer, you can add a method on the parent to add children:

function Group() {
    // ... other code ...
    function addPeople(data) {
        this.collection.push(new People(data, this);
    }
}
3
  • This creates a circular dependency.
    – Adam
    Jul 9, 2013 at 9:24
  • @Adam: how so? Group doesn't depend on People, does it? Jul 9, 2013 at 9:34
  • 1
    Well, yeah, Group will have a collection of People (I'd rename it to Person), and one People object will have a reference to one Group. But that's not really a problem (not like circular references in .NET). In fact, it's often used, and sometimes called a bidirectional relationship (specifically in ORM-circles, where it's often used for collections, i.e. in NHibernate).
    – Peter
    Jul 9, 2013 at 10:22
1

You can mimic inheritance by assigning a new Group object to the People.prototype

function Group() {
  this.address = 'EveryWhere';
  this.Collection = [];
} 


function People(data) {
  this.name = data.name;

  this.getAddress = function() {
    return this.address; //how to access it (declared in the Group object)?
  }
};

People.prototype = new Group();

var Serie = new Group();
var John = new People({name:"John"});
console.log(John.getAddress());
1
  • 1
    Is this a good idea? Setting up a prototype inheritance sort of implies that (the some what odd named) people object also is a group - that it will have it's own Collection array and everything. This doesn't seem to be what the original poster wanted.
    – AHM
    Jul 9, 2013 at 9:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.