02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

Creating a JavaScript Constructor

Yes its time. Now that we created and worked with a few objects its time for us to know how to create our own objects. Constructors in JavaScript are very simple if you know what a function is then you know how to create a constructor. Now that you know its easy to create lets now talk about how to turn functions into constructors. Every function can be a constructor its that simple. Only when we actually trigger the function does javascript actually define it as that. The step involved is by calling the 'new' key. By the end of this session you will know how to create a constructor, you will know how to build objects based on this constructor and define them as a variable.

We start talking about 'this' and what it actually means. The this is not a static value it always returns to us what we are when we are in it. And as such when we call the this operator depending on where we are in our code we will get a different value returned. The nearest object we are in is always returns to us. Although we don't talk about it much at this stage pay a lot of attention to the key word this as it will help you a lot through out the course as we start in later sessions about scope.

Video Transcript

00:00All right, so it's start to start doing the fun part, which is the actual creating of objects so far
00:06We've said we've been lending off the idea of a constructor
00:09But haven't really seen what it is and you'll be surprised how simple it is in JavaScript
00:15Alright now before we're gonna do that. I'm gonna update in our HTML file
00:19I'm gonna update our source file to be just a new file, which is the exact same file duplicated
00:24I've done that between the videos. I'm just gonna click on the refresh button here and dream waiver
00:28So we'll get the new file so we could jump right into that new file, which is the exact same place that we left off earlier
00:35And I want to show you really I'm gonna start with it actually building a constructor with you and let's call this constructor
00:41And by the way a naming convention with classes in our case in JavaScript. There's no such thing as the class
00:48But for our constructors, we like calling them in an uppercase
00:52So it's really easy for us to know that they're not a regular function
00:55But they're actually a constructor function in our case that the the class double quote
01:01That's gonna create our object and again in JavaScript
01:04There is no such thing as classes, but it's definitely an object-oriented programming language. It's just done in a different way
01:10All right. So we're gonna create a new constructor and you're gonna be really surprised if you don't know this already
01:16Every function could be a constructor. It's that simple. So I'm gonna create a new constructor function and I'm gonna call this our
01:24Let's say this our
01:28Map
01:30Student map, all right
01:32We're gonna call it student map and student map is gonna be our our goal in the end of this session is to have a map
01:37With our students where students could actually add themselves into the map letting us know that they're one of our students here in the
01:45worldwide
01:46Web or actually world. All right
01:49We created our new function and really that's all we had to do to create our first constructor
01:54Congratulations, you've just built your first constructor and you could actually already right now start using it
02:00And I want to show you just how easy and how real this is
02:03So I'm gonna go right into my function and in my function. I'm just gonna create here
02:06I'm gonna call my console that log a new object has been created a
02:14New object has been created and I'm just gonna in the last line of our init function. Whoopsie
02:20In our last line of our init function. I'm just gonna set here a new variable
02:25It's gonna be an SP as M for student map and I'm gonna set up a new student map open close bracket
02:33Finish it up go right into our browser and see what happens easy in line 18. We're actually creating that new
02:40new object
02:41And all that's left for us to see is right if we're actually getting that console
02:45I'll add up that log whenever we create that new object
02:48So I'm gonna jump right into our Safari
02:51So I'm gonna jump right into our Safari and I'm just gonna refresh but before I do that
03:00I'm just gonna make sure that I'm gonna open up my error council gonna put here right here on the side here
03:06So I can easily access it gonna click on refresh and if we go into our council
03:11So you'll see a new object has been created and it's true
03:14We have just created a new object and you might be thinking and you might be telling me and you probably will be right
03:20How do you know that it's really a new object it could be just the fact that we call the function student map, right?
03:25So this is where it moves us on from the easiest part, which was building constructor. You just build a new object in
03:33JavaScript
03:34Now there's one thing that you need to know about which is scope now
03:37You might have encountered scope earlier even before you work an object oriented programming
03:42But scope becomes more and more critical when it comes down to working an object oriented programming as you're traveling in your code
03:50You always need to know what are you in when you're working an object oriented programming?
03:55Just as much as when let's say whenever we're inside of this area that is highlighted
04:00We're inside the init functions. Our scope is the init function
04:04Same is true when we are inside of an object
04:07So now that we're inside of this function, not only we're in the scope of this function
04:12We're also in the scope of the object itself that is going to be created when we create that new object
04:18Aka if I put your comma and trace out this into our console. We're gonna get the object that we're in. That's our scope
04:27all right, so if I run this just one more time and I go into my browser and just hit the refresh button and
04:33Look at what's coming out. You'll see that my student map is the object that I have selected. That's the object
04:40That's my this now another thing that you want to note now that we're talking about scope
04:45If I would trace here if I would go to my console
04:48Log and I would trace out this when I'm not inside of the object. I'm inside of something else
04:53I'm gonna get a different result that this is not a static thing
04:57It returns to us what we are or where we are whenever it's being called thus in line 19 when we're gonna be calling
05:04This this what we're gonna call to this
05:07Operator we're gonna get a completely different result. So back into our browser and
05:12We'll refresh just one more time there
05:14We'll see that now we're getting the window and the reason is is because we're not in a specific object
05:19It's searching for the nearest object and the nearest object that we're in is the window which is of the browser itself the window browser
05:26It's a part of the dumb dumb
05:29functionalities

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!