Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Daniel Maia
2,559 PointsHow to access precedent object from an object inside an object??
If I have:
object1 = new function(){
this.color = 'black';
this.self = this;
this.obj2 = new function(){
this.color = 'red';
this.self = this;
this.obj1 = null //But I wish to get the value I would get in a console by running the code: 'object1.color', however, the value inside the variable of the precedent object.
}
}
}
How can I have in the variable 'object1.obj2.obj1' instead of 'null', the object1 itself that precedes the obj2?
Daniel Maia
2,559 PointsIn the only line with a comment, let's say I want to get the value inside object1.color, which is, in this example, 'black', however, I want to get this value (that is within object1 )from an object inside object1.
1 Answer
LaVaughn Haynes
12,397 PointsThis would be super confusing in practice but I assume you are just experimenting. You can do it this way
<!DOCTYPE html>
<html>
<head>
<title>Object</title>
</head>
<body>
<script src="http://code.jquery.com/jquery-1.11.0.min.js" type="text/javascript" charset="utf-8"></script>
<script>
//pass in the parent when creating the object
function GenericObject(parent){
this.color = 'black';
this.child;
//if no parent is passed then the parent is an emtpy object
this.parent = parent || {};
//color is passed to the child
this.createChild = function(color){
var child = new GenericObject(this);
//if no color is passed, color is red
child.color = color || 'red';
this.child = child;
};
}
//new Object1
var object1 = new GenericObject();
//create the 2nd object in Object1
object1.createChild();
//grab object2
var object2 = object1.child;
//create the 3rd object in Object2
object2.createChild('blue');
//grab object3
var object3 = object2.child;
//red
console.log(object2.color);
//black
console.log(object2.parent.color);
//blue
console.log(object3.color);
//black
console.log(object1.color);
//undefined - Object1 is the most top level object
console.log(object1.parent.color);
</script>
</body>
</html>
Daniel Maia
2,559 PointsI'm not sure if that's what I really meant. My doubt was of how I can access object1 in object2 if object2 is an object that was created inside object1. Actually, I can't tell if that answers my question for now, because I'm still trying to understand everything from your example and experiment what really happens in there.
It's kinda confusing, but I realized later on that I could achieved that by requesting a parameter in the creation of object2 and passing the property I want to acquire from object1 (object1, the object that holds object2), and that a quite versatile solution would be to pass the object itself as parameter to object2 (using the keyword 'this'), so I would have access to all the properties of the object1.
If you didn't actually get what I'm trying to explain and you're interested about it, you can tell me, and I can try to exemplify for you in a code (or maybe bring the one that I'm actually trying to apply this, different from the code I wrote in this post, which was a "sandbox test"). Well... Anyway, thank you a lot for the interest and for the code, I saved it and I'll give it some study. It helped me realize some mistakes and misunderstandings I was doing until I ran studied that code more meticulously.
Thank you a lot for the help.
LaVaughn Haynes
12,397 PointsI was basically making an object that creates a child object with access to it's parent. Here are a few shorter and simpler alternatives that accomplish what I believe that you are trying to do
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<script src="http://code.jquery.com/jquery-1.11.0.min.js" type="text/javascript" charset="utf-8"></script>
<script>
var object1;
//original (your original code)
object1 = new function(){
this.color = 'black';
this.self = this;
this.obj2 = new function(){
this.color = 'red';
this.self = this;
this.obj1 = null //But I wish to get the value I would get in a console by running the code: 'object1.color', however, the value inside the variable of the precedent object.
}
}
console.log(object1.obj2.obj1);
//option 2
//use object literal as inner object
//removes confusion with "this"
object1 = new function(){
this.color = 'black';
this.obj2 = {
color: 'red',
obj1: this
};
}
console.log(object1.obj2.obj1);
//option 3
//save the first object in a variable
//removes confusion with "this"
object1 = new function(){
var self = this;
this.color = 'black';
this.obj2 = new function(){
this.color = 'red';
this.self = this;
this.obj1 = self;
}
}
console.log(object1.obj2.obj1);
//option 4
//move the function that creates the inner
//object outside of the function creating the outer object
//removes confusion with "this"
object1 = new function(){
this.color = 'black';
this.obj2 = new SubObject(this);
}
function SubObject(parent){
this.color = 'red';
this.obj1 = parent;
}
console.log(object1.obj2.obj1);
//option 5
//have the outside function create
//both objects
function TheObject(property, parent){
this.color = 'black';
this[property] = parent || {};
}
object1 = new TheObject('obj2'); //this.obj2 = {}
object1.obj2 = new TheObject('obj1', object1); //this.obj2 = new TheObject()
console.log(object1.obj2.obj1);
</script>
</body>
</html>
LaVaughn Haynes
12,397 PointsLet me know if that helps. Otherwise if you think we are not on the same page feel free to provide the example code
Daniel Maia
2,559 PointsYep, that's what I was trying to understand. I will save your codes so I can practice whenever I need to, since I'm still messing up, but yeah, that's basically what I was trying to understand.
It's funny because when we use an object initializer, the value in 'this' continues being the outer object, but in a constructor function, the value 'this' becomes the object itself (in this situation, instead of the outer object, the inner object). I will still confuse this a lot. I found option 3 the most efficient, and probably the exact solution I could expect. That's the one that works the best with me. The option 4 works as well, but it separates too much one from another, which will be useful in different situations than from the option 3, but I'll still take that in consideration. The option 5 is unnecessarely complex (in my opinion), and though it works, its more useful for other purposes.
But thank you a lot for the help. That clarified a lot of things for me. I don't know in what sources I would ever find the solution for the problem I was having, so, well... Thank you a lot.
LaVaughn Haynes
12,397 PointsLaVaughn Haynes
12,397 PointsI'm not sure what you are trying to do. Are you trying to return an object from a function?