Dot Syntax
Practicing the dot syntax way of approaching properties and methods of objects.
Video Transcript
00:01In object-oriented world, and especially in ActionScript 3 and 2, but definitely in 3, which is our focus,
00:08the main way to approach properties, which is variables of objects, is with the dot syntax.
00:15Let's see how that works. So let me first create a new variable.
00:19And it's going to be a type of O, which is a type of object.
00:23And we're going to create a new object.
00:28Notice, don't forget, it's an uppercase O, and all the rest is lowercase.
00:33We're going to open up a bracket, and we're going to close a bracket.
00:38And we're going to put a semicolon, as we've always done in every line of our code so far.
00:44Now, after we've created, by the way, O2O, it's just a side note.
00:49Semicolons in Flash are really just for us as the users. Flash doesn't need them.
00:55Flash knows when you break a line, it's a new line of code.
00:58The semicolon is kind of a tradition from a lot of different programming languages,
01:02where the compiler, which was running the application, had no clue about when does a line end.
01:09And the best way for it to know when you actually meant to finish a line of code was by using a semicolon.
01:15And in that way, it kind of became a tradition where the semicolon is just used in Flash,
01:20and it's used in many, many other programming languages.
01:23When we're done saying what we want to say, we use a semicolon.
01:28Quick reminder, we've seen how we could put an associated array,
01:32which in our case, if we would want to say Ben in our associative array,
01:38and we want to put in, oh, we made a little mistake there, right?
01:41Close quote, close bracket.
01:43And we could have put a value in it. We could have put any value.
01:46Could have put another string inside, right?
01:48So I could have put a string inside that says, Ben, all in upper case.
01:53So we've seen how to do that.
01:55Let's see how we actually would work with a dot syntax.
01:58The dot syntax is much easier and much more friendly on the compiler,
02:02because it makes it easier as we're going to go along and work on other things here.
02:07It's going to make it easier for Flash to realize when we write something that we actually mean,
02:13and when we don't.
02:15And more accurately, when does that thing exist and not based on the definitions inside of Flash?
02:21And that's a bit vague right now, but don't worry about it.
02:23As we build different types of objects, it's going to be really, really clear,
02:27and we're going to repeat that.
02:29So let's see how we do the dot syntax.
02:31As the name implies, instead of opening a bracket
02:33and putting a string inside between the brackets,
02:36all we have to do is type dot.
02:39After we type a dot, we could type in our new property.
02:42Because we're inside of an object, we could type whatever we want.
02:45Objects are dynamic. We could put anything we want in them.
02:48So in this case, if we wanted to create a variable,
02:51and to type ben inside of our object, we would say ben, then equals, and output ben.
02:59And let's just see that it actually works.
03:01We could actually trace it out.
03:03We could trace our oben, and if we run our application one more time,
03:07we're going to see that it does recognize the value inside of oben.
03:13So this is our really, really first introduction to the dot syntax,
03:16even though we ran through the dot syntax in the functions videos,
03:21where we worked with the math class, which was built in math operations.
03:26And we worked inside of there where, if you remember, we used the abs function.
03:32And the way we accessed it was we used the dot syntax,
03:35where we typed math.abs, which is a function.
03:39And you could see in the hints that our function would take a number
03:42and return a number.
03:44And if we would send to it a negative value,
03:47and we close our brackets, to make sure that we won't get any errors,
03:51we close our bracket for our abs, we close our bracket for our trace.
03:55And if we run our application this time around,
03:57we're going to see our trace would return 50,
03:59because the math.abs returns a value, an absolute value,
04:05for any number that you put into it.
04:08So we met the dot syntax earlier, but we kind of ran through it,
04:11and we didn't really focus on it.
04:13But it's a very important block, mainly in Flash.
04:16It's true in a lot of other object-oriented programming languages,
04:20but not all.
04:21It's a much, much more easier and palatable way of working,
04:24in my opinion, and probably of many other people that are used to it.
