0

I have a slideshow class looking like this:

//Our slideshow class
function Slideshow(moduleId) {

  this.moduleId = moduleId;

  this.activeSlide = null;

  this.slides = new Array();

  this.newRound();

}

and I instantiate it telling it what module it is on like this

self.modules[moduleId].slideshow = new Slideshow(moduleId);

Now as you can see, it is already "mounted" on a module named by the moduleId, so my question is: Is this object (the instance of Slideshow) aware of it's parent?

Can I find out the moduleId by doing something like

parent.name

and get the moduleId that way?

Matt Welander
  • 8,234
  • 24
  • 88
  • 138

1 Answers1

0

No. Because JavaScript passes objects by reference, the same Slideshow object could actually be referenced by several "parent" objects simultaneously.

Stephen Thomas
  • 13,843
  • 2
  • 32
  • 53
  • ok, so the only way really for slideshow to know what "node" or "moduleId" in my case it os "mounted" on is to do what I did? – Matt Welander Jan 18 '14 at 21:26
  • Right. If you want to maintain a reference to another object within the Slideshow object, you can pass a reference to that object (whether it's a "parent" or not) in the constructor. – Stephen Thomas Jan 18 '14 at 21:27
  • From a similar question on StackOverflow: "A nested object (child) inside another object (parent) cannot get data directly from its parent." (http://stackoverflow.com/questions/2980763/javascript-objects-get-parent). One option is to pass the Slideshow object as a parameter to the modules's constructor, which could set up a 'parent' property on the Slideshow. – Michael Zalla Jan 18 '14 at 21:27
  • Super, that totally solves my problem, I'll pass a reference to the parent in the constructor. Many thanks! I guess I will still need the moduleId passed like I have it in the constructor as well, but this is great news anyways. – Matt Welander Jan 18 '14 at 21:43