This course has been updated into a 4 part series! We now recommend you take the A Practical Guide to Algorithms with JavaScript course.

Check out a free preview of the full Data Structures and Algorithms in JavaScript course:
The "Creating a Constructor Solution" Lesson is part of the full, Data Structures and Algorithms in JavaScript course featured in this preview video. Here's what you'd learn in this lesson:

Bianca walks through the solution to the Creating a Constructor exercise.

Get Unlimited Access Now

Transcript from the "Creating a Constructor Solution" Lesson

[00:00:00]
>> Bianca Gandolfo: What's your example?
>> off screen male: We made a yurt.
>> Bianca Gandolfo: A yurt, nice.
>> off screen male: So I took one function, which is the number of ribs, which are poles, I guess.
>> Bianca Gandolfo: [CROSSTALK] Reduce.
>> off screen male: That's about it, we have a website, MyYurt.io.
>> Bianca Gandolfo: [LAUGH]
>> off screen female: Patent pending.
>> off screen male: Patent pending.

[00:00:15]
>> Bianca Gandolfo: Wow, I like it. So here's our yurt, and what does our yurt take? It takes ribs. Like this? Like barbeque? Mm.
>> Bianca Gandolfo: All right, great. So here we are. We have our constructor function. We're using the keyword this. That's where we define our properties, everything else is just magic.

[00:00:47] We have biancasHouse, cuz I want to live in a Yurt when I grow up, for sure. New Yurt, and then how many ribs on estimate, 50?
>> off screen female: You said 16, but you can have 15.
>> Bianca Gandolfo: I'll have 50 ribs because why not. Wow, that's a big yurt.
>> off screen male: It's a full rack.

[00:01:08]
>> Bianca Gandolfo: Mm-hm.
>> off screen female: [LAUGH] She's going to meet you.
>> Bianca Gandolfo: I'm hungry. All right, so now I have a yurt, it has lots of ribs, barbecue ribs. I am from the South, originally, so I can get behind some ribs in my yurt. We'll say that's a good combination of my Southern and my Berkeley just like, all together.

[00:01:30] We also wanna have some things on our yurt, right? Some things that our yurt can do. What can our yurt do?
>> [INAUDIBLE]
>> off screen female: Our yurt can count the ribs.
>> Bianca Gandolfo: Count ribs?
>> off screen female: Yeah, we should've made it do something cool. [LAUGH]
>> Bianca Gandolfo: So if you have more than ten ribs, we're definitely gonna console.log.

[00:01:57] Yum, hello, absolutely, uh-oh. Also please feel free to correct my live coding, because sometimes in the moment, just kind of, it just falls out of my brain. So here we are. So our main components here, that we don't want to forget, we have our constructor function, we have our key word this, that's where we define our properties.

[00:02:22] We have our methods. And again, your going to have lots of properties, lots of methods on your data structures and the operations for them. We have a function here. Key word this here, in this function, is gonna be referring to the instance, yeah? That's kind of confusing sometimes, .countRibs, right?

[00:02:53] It's gonna say, well this was always gonna say young, right?
>> Bianca Gandolfo: But the keyword this, it's like our pronoun. It's gonna be the instance. That's not always true. There are a few different rules for the keyword this, but that's the one that we're gonna follow today. Awesome, any questions?

[00:03:22] Yes.
>> off screen male: Steven, he has a request that you'll also show the ES6 or ES2015 syntax.
>> Bianca Gandolfo: I don't know it. Sorry, I'm not an ES2015 kinda girl yet, there's still time. However, I haven't gotten there.
>> Bianca Gandolfo: Do you have equa script 2015 class, Mark?
>> off screen male: Everybody still calls it ES6, but yeah we do-

[00:03:47]
>> Bianca Gandolfo: Yeah, I'm trying to get in with the-
>> off screen male: Yeah, we do.
>> Bianca Gandolfo: Okay.
>> off screen male: Aaron Frost.
>> Bianca Gandolfo: Yeah, so for those of you who are totally into the ES6 thing, Aaron Frost's class, you should check it out.