This course has been updated! We now recommend you take the JavaScript: From First Steps to Professional course.

Check out a free preview of the full JavaScript: From Fundamentals to Functional JS course:
The "Constructors" Lesson is part of the full, JavaScript: From Fundamentals to Functional JS course featured in this preview video. Here's what you'd learn in this lesson:

Bianca introduces the concept of using a function as a constructor. A constructor is a function that returns an object. This object is a data model which contains the properties and methods for that object.

Get Unlimited Access Now

Transcript from the "Constructors" Lesson

>> [MUSIC]

>> Bianca Gandolfo: I'm just gonna jump right into looping. And well, I'm sorry that's a misnamed slide. We're gonna talk a little about constructors and we're gonna be using loops and we're gonna build on this more tomorrow when we get into the functional programming. So we're gonna do some things here just the manual way, which tomorrow, we'll sort of apply some functional approaches using the underscore library.

[00:00:24] But the first thing I want to talk about is the idea of a function as a constructor. And as we see, we have this function called AnimalMaker on the screen. We have the capital A to let other programmers know that this is a constructor. And what a constructor is, at its core, is a function that returns an object.

[00:00:44] That's it. So we see here we have our AnimalMaker, it's just returning a function. I'm sorry, it's returning an object. And what does this object, what properties does this object contain?
>> Speaker 2: Speak?
>> Bianca Gandolfo: Speak, yeah. So speak, and it's a function that just console logs the name of the animal.

[00:01:09] True? So, then if we wanted to create an animal object, so var my animal, oops.
>> Bianca Gandolfo: Equals.
>> Bianca Gandolfo: Just a second. To put our function in there. So var myAnimal = AnimalMaker. And then we could pass in a name.
>> Bianca Gandolfo: Who has a pet that they love?
>> Bianca Gandolfo: What's it name?

>> Speaker 3: Cheetah.
>> Bianca Gandolfo: Cheetah? Cheetah or Cheeto?
>> Speaker 3: Cheetah.
>> Bianca Gandolfo: Cheetah, okay, like that.
>> Speaker 3: She looks like a Cheetah?
>> Bianca Gandolfo: Cool. It's a kitty. Is that how you spell cheetah? Yeah, okay. So now, we just used our constructor function to create our animal object. And here we are.

[00:02:06] And then, if we then wanted our animal to speak, how do we go about doing that? Yeah we could use a dot, and then what? Yep, good job. .speak like that. And what's this gonna return?
>> Speaker 4: A function. No, there is a return isn't there?
>> Bianca Gandolfo: What's gonna happen?

>> Speaker 5: Don't we have to put the parentheses in there?
>> Bianca Gandolfo: Yeah, absolutely. So if we don't put the parentheses, we just get a function, and that's not really what we expected, right? We actually, we wanted to do what we expect it to do, which is to console.log("my name is ", name), speak.

[00:02:58] And so it says, my name is Cheetah. Cool? So that's the essence of a constructor function. And so we're taking some information and we're turning it into an object that we then had, then we have a model of our data that we can use in some way. And notice the rules again, they don't change, the ways that we access our methods are the same.

[00:03:23] We can use dot notation, we can also use our bracket notation, like this guys.
>> Bianca Gandolfo: Like this. What am I missing?
>> Speaker 6: Quotes.
>> Bianca Gandolfo: Quotes, exactly.
>> Bianca Gandolfo: There we are. So and then I was talking with a student the other day about.
>> Speaker 7: So and Britt, I'm sorry, in back of notation you had the invocation also?

>> Bianca Gandolfo: Yeah.
>> Speaker 7: Okay.
>> Bianca Gandolfo: Absolutely, yep.
>> Speaker 7: Okay.
>> Bianca Gandolfo: That's important.
>> Bianca Gandolfo: So
>> Bianca Gandolfo: So I was talking with someone the other day about when you inspect the inside of a function here, it's not saying Cheetah, it's saying name. We got into a discussion about why doesn't it just say Cheetah if it's gonna be Cheetah?

[00:04:23] And the answer is, is that before you call a function your parameters don't have a value, remember? And so even though we know that it's gonna be Cheetah, that variable gets its value dynamically when you call the function. And so that's something to note when you're going through your code.