The Object object
So far when we worked with objects, we worked with objects that were useful right on their own, Like the Data object. It's time for us to start creating and not only using objects. To do that we need to discover the Object object. The object object is the most basic of objects. In many ways the object object is an empty canvas. In this lesson we're going to see how to create objects in a theoretical matter before we go back to using it for animations.
Video Transcript
00:00In the last chapter, we worked with objects and by now I think you know what objects are and how to work with them.
00:06You've even seen how to instantiate them.
00:08But so far, we started up from objects that already had some functionality in them.
00:14What if we wanted to create a function?
00:17We wanted to create a bucket and start adding into a functionality.
00:20And that's exactly what this chapter is about.
00:23So let's jump right in and see how do we create our first object.
00:28Well, our first object that is actually empty.
00:31And it's called object.
00:33Pun intended.
00:35I want to start talking about how to create your own object.
00:38So to do that, I cleared everything out.
00:41And when I say create your own object, we have already seen how to create an object, which was the new date object.
00:47But the new date already has a lot of stuff in it.
00:50And the idea behind, like we said, with all the other objects we saw, they hold inside of them.
00:56A plethora of things that are relatable or related.
01:00Now, how do you create your own object?
01:02How do you add to variables?
01:03How do you add to it functions?
01:05And that is exactly the topic of this lesson.
01:08Now, so let's get started with creating that object.
01:11And as no surprises there is you just set the variable.
01:15In our case, I'm setting it as O for object.
01:18And I'm just typing the word new and the word object.
01:21Not whatever.
01:22I just typed there.
01:23Object.
01:24There you go.
01:25Uppercase O.
01:26And that's it.
01:27We created a new object that is basically empty.
01:31It has a few built in functions and properties that every single object has.
01:56We're not going to get into inheritance and all those fancy stuff for this beginner JavaScript learning animation title.
02:03All you really need to know with objects at this stage is how to work with them.
02:07So once you created that object, once you said I want to have a new object, that's it.
02:11You have an object.
02:12This item is an object and it has the capability of storing variables and also storing functions.
02:19So one way of storing a variable is, for example, I could set here and how about we'll call this object.
02:24Let's say, oh, how about we call this object just sample for now just to showcase the different variables.
02:37And then we'll go back to creating an object that will be a little bit.
02:40We'll have a little bit smarter stuff inside of it.
02:43So first of all, I created my sample.
02:45I can now approach my sample and say, hey, I want to put your variables in one.
02:51I'll set the value here to value.
02:54And literally, that's all I had to do.
02:56Just like the way I approach variables in an object, I could also assign them a value.
03:02So I could go ahead and just put here a trace for my sample that var one.
03:07And notice how I'm not calling it var because var is a reserved word for creating new variables like we have right here.
03:13In the case of a variable of an object, all I'm doing is just setting that dot and then the name of the variable.
03:19And that's it.
03:20It's inside of that variable and defining whatever value I want it to have.
03:24And if I just, not trace, sorry, I'm used to other languages, so console.log.
03:29And if I save that and I go back into my application and this time around load up my starter file for chapter five.
03:37We'll see that this time around, I'm going to get that value.
03:39It's going to come right in.
03:41That's exactly how I create a new variable in an object.
03:44And I could create many variables.
03:46So sample a variable to I could give it whatever name I want.
03:50And I could send here another value.
03:52In this case, I put your two strings and I could output both of them at any stage and work with them.
03:58Now, let's say how do you add a function into an object?
04:00We already know how to create functions.
04:02So literally, the process is very similar.
04:04All I have to do is approach my object, define the name of that function as if it was a variable.
04:10So let's say I want to call this output vars, var one and two.
04:18Now, another something we haven't really ever talked about, but we never want to start a name of a variable with a number.
04:24It's a no no.
04:25Just like we wouldn't create a variable called var one.
04:29We'll always start with a numeric value.
04:32We're not going to start.
04:34Some also would use an underscore or a dollar or a dollar.
04:38Don't just use just use characters.
04:42You could always use a number once you're ready.
04:44Set the first character so you could put numbers inside later on.
04:48I personally prefer is just basic.
04:49It's always English uppercase, the next word.
04:52And you could put a number in a case that you need a number.
04:54Most chances are you wouldn't need a number.
04:56But in this case, because we have such generic names, which is never a good idea.
05:00If we actually knew what these variables contain, usually want to have a descriptive variable that's usually characters.
05:06So there we go.
05:07So we created a function.
05:08We're creating a function called output var one and two.
05:12All that's left for me to do is to set the equal sign and then create here a function, but not give it a name.
05:19Open the curve, the round bracket, close the round bracket.
05:22And I could do whatever I want inside here, such as console dot log.
05:26And I'm going to log my.
05:30Oh, oh, by the way, this is beautiful.
05:32So now that I'm ready inside of this object, I can't refer to it as sample because sample is the name of the object when we're talking about it from outside.
05:44We don't really know it.
05:46Theoretically, we don't know its name.
05:48So instead of approaching in its name, we will approach it from here on as this when we're inside of that object.
05:56And then I could approach this and say var one.
05:59I want to log var one and I want to log var two.
06:04So I'm using the word this instead of using the actual name.
06:07And this actually means the object that I'm inside.
06:10Now that I'm actually working inside of a function that belongs to an object, this is our first method.
06:16And as we said in an earlier chapter, when we were talking about functions, we said there's basically three types of functions.
06:23We met two of them before.
06:25One of them was a procedure where he said it just does stuff, but it doesn't return anything.
06:31We said there's a function which actually returns things and we've worked with a lot of them so far.
06:36And then we said there's a method or now we're saying that there's a method and a method belongs to an object.
06:42We've worked with a lot of methods so far.
06:44This is the first method we're creating ourselves.
06:47So now that we're approaching a function that belongs to an object, we don't need and we don't want to use the direct name of that object.
06:55Instead of that, we want to just approach it with just saying, hey, I want to talk to the object that this function belongs to.
07:02And by saying the word this, that's exactly what I'm doing.
07:06So let's give it a go.
07:40All that's left for me to do is to create here and I'm just going to comment down that console log.
07:45And instead of that, I'm going to call O sample and I'm going to call output vars one and two and just trigger that, save that, run my application.
07:54And hopefully we're going to get outputted the two variables, the two values, and we sure are getting those two variables.
08:00So there we go.
08:01We just created a complete object with its properties and with its first method we've ever created.
08:10Now, what I want to do is before we wrap up this video, I want to show you the shorthand way of creating the exact same thing.
08:17Now, we worked with the shorthand object before, but we haven't probably noticed that we did.
08:22But I want to show you the steps involved with it.
08:25So I'm going to go ahead and I'm going to create here another variable and I'm going to call it O sample two.
08:31Now, O sample two is going to be a new object as well.
08:37But instead of creating a new object, I'm going to use a shorthand way of creating a new object.
08:42And the reason is because there is a lot of times you want to create new objects.
08:46So there's a shorthand way of creating really quickly a new object.
08:50And that's what you do.
08:51Open curly bracket, close curly bracket, and you've just created yourself a new object.
08:56Notice how similar that is to when we were creating functions and when we had a curly bracket and when we were using the if command,
09:04we saw how the curly bracket was used to bound an area where things were inside of it.
09:11And very logically, the things that were inside of it, the idea behind it is very similar where we're bounding the object with the things that are inside of it.
09:20In this case, there's nothing inside, so we're bounding an object that is empty.
09:24But if we want to, and we sure do, we could start putting in the properties.
09:28So in this case, if I have your var one, I just have the set var one, and then I put here a colon,
09:33and then I could put inside the value of var one right into that new object that we're creating,
09:39and it's going to create it at the same time.
09:41The next thing, if I want to put another variable, I could just put here a comma,
09:45and I'm just going to put an enter, although it's not mandatory.
09:47I'm just doing that for aesthetics, so it's easier for us to see what's going on.
09:51And for var two, I'm going to do the exact same thing.
09:54And last but not least, just to showcase the fact that we could also put inside functions,
09:58because functions are basically smart variables.
10:01So I'm going to go ahead and just create here that variable name of output var one and two,
10:07and just define its value as a function, literally creating an open and closed rounding bracket for my function.
10:15And I'm basically just doing almost the exact same thing,
10:18and the only difference is that we have this semicolon right here.
10:22And all that's left for me to do is to put the value inside of here, and there we go.
10:26We've just created another example object using a little bit less code,
10:33and this is a very, very common structure of way of seeing how objects are built.
10:38A lot of times you'll see them without those break lines,
10:40but if you just break the lines, it'll make it easier for you to see them.
10:43So if we want to just test this out, we could just test out our sample two,
10:48just to see that we're going to get that output come back as well.
10:51So I'm going to just put here just value two,
10:53just so you can see that it's coming from the second object that we've created.
10:58And I'm going to go ahead here and click on refresh,
11:01and we should see now value two and another value two.
11:05Alrighty, so that's it.
11:07That is our talk about how to create objects.
11:09We've seen the longhand way of creating them.
11:12We've seen the shorthand way of creating them,
11:14and every variable here that is living inside of this object
11:18is exactly like a regular variable.
11:21It only belongs to an object.
11:23So I could go to this variable and add things to it,
11:25subtract things to it in the exact same way
11:28as I would approach a regular variable.
11:30The only difference is now it belongs to an object.
11:33Now, before, in the next lesson actually,
11:36what we're going to do is we're going to see these objects again in action,
11:39but this time around we're going to approach our tween max and create animations,
11:43and now that we know how to work with objects,
11:45we'll start creating animations that are even more sophisticated.
11:49So before we wrap up and before we close this topic,
11:52I just want to say a couple more small things.
11:55So one is if this is a little bit confusing, that's fine.
11:59Just take a break.
12:00Go ahead and create your own object.
12:02Obviously, it'll be great if you create your own object
12:05that has essence.
12:06Right now we just created samples.
12:08In the following few videos, that's exactly what we're going to do
12:11is we're going to take this core thing that we learned.
12:13We're not going to learn new things,
12:15but we're going to integrate it into all the stuff that we've done so far
12:18to create something that animates.
12:20But we're going to start off first looking at how do we integrate objects
12:24when we're working with our tween max.
12:26And we'll see you in the next lesson where we start practicing all these stuff.
12:29So don't worry if it's a little bit vague
12:31because throughout the rest of this chapter,
12:33it's going to become more clear and more clear.
