Primitives and Complex Data Types
Going back to primitives and comparing them to complex data types(our object).
Video Transcript
00:00Let me just create here a couple variables.
00:03So let's start with a Boolean value.
00:04Is valid, which will be a type of Boolean.
00:11And let's see what type of options are is valid
00:14our Boolean has.
00:15And you'll see that our Boolean has quite a few
00:18interesting properties here.
00:19And the most interesting part about all this thing
00:23is that our Boolean has a lot of things
00:26that are basically the exact same thing
00:27as our object has.
00:29And that is because everything, it
00:32works with inheritance in Flash.
00:33So everything inherits properties from the object class.
00:38And although our toString would give us a different value
00:40than what our object would give.
00:42And if you notice, you wouldn't see this
00:44if you're working in different versions of Flash.
00:46But if you look at this right here,
00:48you're going to see the hasOwnProperty whose
00:52definer defined the object.
00:55So you could see that all these functions have been
00:58predefined in the object.
00:59And you could see that maybe here the toString,
01:02the Boolean itself is implementing.
01:05The to, the value of the Boolean itself
01:08is implementing, and so on.
01:10But it's really not important right now, actually,
01:12the functions, because the whole point
01:14of this whole little really, really small sample
01:17is just to show that every single type of variable
01:20in Flash has methods associated with it by default.
01:24And we're going to play around with a lot of those methods
01:27really, really soon.
01:29The next thing I want to talk about,
01:31we're still in this world of objects.
01:32And we're still playing around with what are these objects.
01:35And let me just delete this away.
01:40Now, we need to regroup and go back to the beginning
01:44for a second now.
01:45And we talked in the beginning when
01:46we were talking about our Booleans and our integers
01:49and our numbers and our strings.
01:52We called them a certain type of object.
01:54We called them a primitive type.
01:57They're primitive values.
01:59What does that mean, primitive values?
02:01And as we've said, and as we built our object,
02:03now we could compare between our primitive values
02:06and our object.
02:09So let's see it in action.
02:11If I actually create a new variable,
02:12and we'll go to our primitives here for a second,
02:15and then see the big difference between a primitive
02:17and a real object.
02:21OK, so our primitive values, which
02:22there are very few of them, let's go for a string
02:26for now, let's pick a string.
02:29We're going to give it a value of Ben.
02:32We're going to create another string.
02:37And we're going to give it the value of someone else.
02:43String has a value of Ben.
02:45String 2 has a value of something else.
02:47If I say to my string, str2, you're equal, str.
02:53So now the value inside of Ben will
02:55be copied into the value of number 2, as we've seen before.
02:58But what would happen if I would go to str now
03:01and change its value to be Bill?
03:06Will the value inside of str2 change as well to Bill?
03:10Are they linked together?
03:12Did we create a linkage between str and s2?
03:14Did this create a linkage between them,
03:16where the values of str, as they change,
03:19would change into 2 as well?
03:22The answer is no.
03:23They wouldn't.
03:23Why?
03:24Because it's primitive values.
03:27And primitive values are basically
03:28when you assign a value, you're
03:30grabbing the value, explicit value,
03:33dumping it into string 2.
03:35So if I now trace out string 2, string 2
03:39will still contain the last value that
03:42was assigned to it, which was the value of str at the time.
03:47And the value of str at the time was Ben.
03:50So if I run my str2, we should still get the value of Ben.
03:54Let's see how that works with objects.
03:55So I'm going to create a new object right here.
03:57And I'm going to call it 01, which
03:59will be a type of object.
04:02And we're going to create a new object right here.
04:07And we're going to take our 01, and we're
04:10going to give it a value.
04:11We're going to give it a new property.
04:13And that property will be str.
04:16And we're going to put a value of Ben in our str.
04:20Let's create another object now.
04:24And our 02 is going to be a type of object as well.
04:26But what we're going to do right now,
04:28instead of creating a new object,
04:29I'm just going to tell my 02, you equal 01.
04:33What would that do?
04:35On contrary to what would happen with an str,
04:38with our strings, with our primitive values,
04:41I'm not actually duplicating the values of 01 into 02.
04:47What I'm actually doing is I'm linking between them.
04:50And the reason is very simple.
04:52The reason is primitive values are very, very small and easy
04:55to manipulate and duplicate.
04:58Well, objects, usually we wouldn't really
05:02want to duplicate them that quickly,
05:04unless we explicitly would ask to duplicate them.
05:06Because objects have a lot of information in them.
05:09And actually, what we have actually,
05:12and we don't have to dig too far into what's actually
05:15happening behind the scenes.
05:18But what we're actually doing, without digging too much
05:20into what happened behind the scenes,
05:21because we're in the world of Flash,
05:23and what happens behind the computer doesn't matter to us
05:26as much.
05:27And if it interests you, I recommend very warmly
05:29to read up on it.
05:31But it really doesn't matter for our work right here.
05:34So for our work here right now, all I'm going to say
05:37is that 01 was not copied into 02,
05:41but a linkage, a link between 01 and 02
05:43has been established now.
05:45So now if I would change 01 str,
05:52if I would change this value to be Bill,
05:56and I traced out 02.str, what value
06:03do you think would come back?
06:06The value that would come back is actually Bill,
06:09because we never actually unlinked,
06:13we never duplicated 01.
06:15And 02, all 02 really is, is a reference to 01.
06:19So now if I run my application again,
06:21we're going to see that our second trace message should
06:24be Bill, and not Ben, as it is with our string,
06:27our example with, well, primitive example.
06:35Let's put it here soon.
06:45And if we run it one more time,
06:46we're going to see that.
06:47Although our primitive example would output Ben,
06:51our object example, maybe not objects,
06:54but our object example will actually print out
06:57the latest thing that came into our object.
06:59This is a really, really critical thing to understand.
07:02And as simple as it is, it's one of the most common,
07:06most, most common, no question,
07:08even the most advanced developers,
07:10this is where they fall.
07:11They fall in forgetting that their object
07:14is not being duplicated.
07:16And when they change something in that object,
07:18they're actually changing it in the original object.
07:22And maybe sometimes destroying something
07:23that they didn't mean to destroy,
07:25or adding things into something that they didn't want to add.
07:28So it's very, very important to understand this difference.
07:31And we're going to definitely see this again in the future
07:33and see more deeply into how this works
07:35as we work with the world of visuals,
07:38which are so, so close to at this point.
