This course has been updated! We now recommend you take the JavaScript: From First Steps to Professional course.
Transcript from the "More Nesting Examples" Lesson
[00:00:00]
>> Bianca Gandolfo: [NOISE] So here we have an example of our inner box. So we just have a doubly nested object. We can handle this, right? It's only two. And with property full equals true. What's gonna happen in this next line here? [BLANK AUDIO] How about Ben, what do you think?
[00:00:41]
>> Ben: For what, what's the question?
>> Bianca Gandolfo: What's the line going to do?
>> Ben: Set the property of height of the objects in your box to 10.
>> Bianca Gandolfo: Good guess, any one think something different?
>> Speaker 3: Undefined because innerBox doesn't have quotes around it.
>> Bianca Gandolfo: Yes.
>> Speaker 3: [LAUGH]
>> Bianca Gandolfo: Good.
[00:01:07] Good catch. Seeing if you guys are awake.
>> Speaker 3: [LAUGH]
>> Bianca Gandolfo: There's coffee in the back.
>> Speaker 3: [LAUGH]
>> Bianca Gandolfo: Okay, awesome. So we fixed that. Cool. That will pass. What about the last line there? That's not gonna work. And so, what my request to you is make that last line work without changing the last line itself.
[00:01:37] And we want innerBox2 is gonna be in different object. So, there's a few things you're gonna need to do to make that last line work. You can do it here above it so that that won't throw an error. So I'll give you guys about five minutes to do it.
[00:02:03]
>> Bianca Gandolfo: And also note innerBox2 is a different object. It's not the same as innerBox.
>> Bianca Gandolfo: Did you have a question?
>> Speaker 4: Did you mean to do that without quotes?
>> Bianca Gandolfo: Yes, so this line will not be changed.
>> Speaker 4: Box to be another box. [CROSSTALK] a sibling of inner box?
[00:02:27]
>> Bianca Gandolfo: Yes, exactly. So it'll be another object. So you need to create a new object in there.
>> Bianca Gandolfo: Cool, so our online friend Soncar sorry if I pronounce your name wrong. Answered this, so I'll just go ahead, type out the answer. So, to do this, we first need to create a variable called innerBox2.
[00:02:55] We know that because innerBox2 doesn't have quotes, and we can call it something else even. So we can call it otherBox. And then we have to unbox, put a property called otherBox, and set it equal to an empty object. So we need to do this first so that we have an empty object, on box, to add the full property too.
[00:03:26] So now we say, box Which is our object, and then we say, look, that's a variable. What is that variable? So, it's otherBox, so you can imagine then that this is swapped out, so that would say box, otherBox, which. We know is an empty object here. And then we'll just put in nested property on there.
[00:03:59] I'm sorry, we'll just put a property onto that object, set equal to false.
>> Bianca Gandolfo: And that's the answer. Whoops.
>> Bianca Gandolfo: Any questions on how we got from.
>> Bianca Gandolfo: From just the empty object? From the variable to the empty object?
>> Speaker 5: Couldn't you just assign inner box two to box [INAUDIBLE]
[00:04:38]
>> Bianca Gandolfo: Then it would say, then it would be kind of like saying this, which wouldn't make sense because we don't have But you really don't want, that's called like circular pointing. That's like, something you don't wanna do. But, yeah, you can't really do that.
>> Bianca Gandolfo: Because we don't have a property called box on box, right?
[00:05:09] So you could, create it, I guess, but.
>> Bianca Gandolfo: So Kate asks, when do you know to set a variable to an empty object? Do you always have to do that, is it a best practice thing? Well, if you're creating an object-
>> Speaker 6: Can you repeat the question?
>> Bianca Gandolfo: Sure.
[00:05:41] The question is, when do you know to set a variable to an empty object and do you always have to do that, and is it a best practice thing. So whenever you create an object from scratch, that's what you have to do. There is constructor notation with a keyword new and then a capital O object, but that's not a best practice in general.
[00:06:06] And so, it's better just to initialize your empty object using the curly brackets.
>> Bianca Gandolfo: So what this does is that it just creates an empty object so that you can add properties to it.