Shorthand
This video is mainly focused to outline the shorthand way of creating arrays and objects. until now when creating complex data types we always started with a "new ___();" while in this video we will introduce a quicker shorter way of creating objects and arrays.
Video Transcript
00:00All right, so objects and arrays are one of the most used
00:04and one of the most basic types of variables,
00:07just as much as numbers and booleans and strings are.
00:11Now there's quite a lot of steps in creating them
00:13and we've seen already how to create an array
00:15where you have to say the new and then the word array
00:18and then to add properties into,
00:20we're quite a lot of work if we wanted to push
00:24and then we added a value and so on and so forth.
00:28Same was with our object.
00:29Our object was a bit of work for something
00:32that we have to do over and over a lot of times
00:34and as you start developing more and more,
00:36you're gonna see that the object type,
00:38although it's very primitive, it's very used.
00:40It's very used because it's a very convenient way
00:42of storing, it's a very convenient way
00:47of storing information.
00:51So because of that, in a lot of different programming
00:53languages there are shortcuts to creating arrays
00:55and there are shortcuts to creating objects
00:58and it's true only when you instantiate your object
01:01or instantiate your array and you already know
01:03what the content that you're expecting to have in it.
01:06Let's start with an array because the arrays
01:07are really, really simple and very easy to create.
01:10So I'm gonna call this A2 just for our second array
01:14and the way it's done, we create a squared bracket
01:18just as much as usually you would access them
01:20and there you go, that's it.
01:21You just created an empty array
01:23instead of saying new space array
01:25and if you're now thinking in your head,
01:27Ben, you bastard, why did you teach us the long way
01:29if there's a short way, is because every other object
01:33beyond the object in the array, there's just no shortcut.
01:36You have to do the long way.
01:38So we wanted to introduce the long way
01:40in the most basic primitive ways
01:41but it's the same thing for you
01:43so it's actually way easier to just do it this way.
01:46It's also, when you get used to it,
01:48it's just so much more fun.
01:49Now you might ask me, could I also start up
01:51with properties in it?
01:53Absolutely, so let's say I wanna put here 10, 20, 30,
01:56minus five.
01:58After I created that array that now has four positions,
02:01so if I do here a quick alert for A2 and ask its length,
02:05we'll see that when I click on my refresh,
02:07we'll see that there's four positions,
02:08exactly the same way as before
02:10and everything else related to array
02:12is the exact same way as we've done previously, all right?
02:16So this is just a quick, short way.
02:18Really encourage you to know it
02:20because if you don't and you look at other people's code,
02:22it's gonna look insanely scary.
02:24If you look at this and you get it,
02:26it's gonna make life so much easier
02:27because a lot of the pro developers use it.
02:30A lot of the people that are comfortable
02:32in JavaScript and in other languages,
02:34such as Flash, ActionScript,
02:36are gonna use this type of syntax.
02:38So get familiar with it, it's really not too bad, all right?
02:42Now, next is I wanna show you how to create
02:44the short-handed way of creating objects.
02:47It's really similar, only a little bit more advanced
02:50because objects don't have index that is numeric,
02:54they have an associative index.
02:56To do that, we have to use a key value.
02:58Now, another way is because it's not an array
03:00and it's an object, we're actually gonna use
03:02the rounded, not rounded, the curly bracket,
03:08the weird one, the one that has,
03:10just the one that you see on your screen right now,
03:11I don't even know how to call this one.
03:13So you could put as many spaces as you want, by the way,
03:15just to make things look cleaner.
03:17You don't have to put spaces,
03:18but the spaces are just for convenience.
03:21And the way you add properties,
03:22you just type in the property name
03:25and you could just put the value directly into it
03:27after putting a colon.
03:29And I'm gonna put here my name.
03:31And I'm gonna put a comma to put another one in.
03:34I'll break a line just for visual reasons,
03:38but obviously, I don't know if obviously,
03:40but the comma is necessary
03:41because we're gonna put another element inside.
03:44Now, the school is gonna be zero2geek.com.
03:49And we'll have one more thing of age of the school
03:54and the age of the school is gonna be 24, not my age,
03:57but I definitely would not mind it if I was 24 right now.
04:01All right, so that covers the way we create our object.
04:05And then later on, when we access our object,
04:07it's the exact same way as we would have accessed it earlier.
04:12Notice that by mistake it created the same variable twice.
04:15It's a bit of a no-no.
04:16You're not gonna get any errors for that,
04:18but it's a very, very encouraged not to do that
04:20because you'll get confused
04:22by overriding your own variables
04:23because then you're gonna start getting things
04:25you might have not expected.
04:27All right, and if I just try to output that name with our 02,
04:30now that I called it 02,
04:31we'll see that we're getting Benfella
04:33and all is good in the world.
