Things are getting complicated
It's not you it's me. Well actually it's us:-). Now that we have under our belt primitive data types and complex data types, it's time for us to realize a very big difference between them. Primitive data types are like Xerox machines they just get duplicated whenever they're called. Complex objects, pm the other hand, are only referred whenever they're called.
Video Transcript
00:00At this stage, you know what primitive data types are and you know what complex data types
00:06are such as an object, because you already created even an object.
00:09So it's time for us to take a break from doing things because we want to talk a few
00:13seconds about something very critical about the difference between objects and primitive
00:18data types in the way they behave.
00:20So let's jump right in.
00:21There is a really important lesson, so important for you to understand.
00:26So I want to talk about the difference between numbers and strings and objects, which obviously
00:32the first difference is, is that objects contain things, they can contain strings and numbers
00:38and functions and other objects as well.
00:41So objects could contain objects, we're not going to look at that right now, but you could
00:45literally take an object, you know, and put it inside of another object.
00:49And we've seen that without even noticing, we've seen that.
00:52But even in this scenario, we have an object that returns a function that returns an
00:56object. So objects are very smart.
00:59Contrary to that, numbers are just what they are.
01:02And what that means, you know, let's see what that actually means.
01:05So when I create, I'm just going to create a quick variable.
01:07So we'll call this OPA for opacity.
01:11And I'm going to set its value to zero.
01:13And I'm going to go ahead and just set that value right here in that element.
01:19Now, notice what will happen if I run my application now and I click on refresh
01:24here, notice how my animation works exactly the same way.
01:27Nothing changed.
01:28Nothing changed because we're literally passing on that value of zero into the opacity
01:33property of the object.
01:36What would happen if we go right underneath our tween, our tween, and I go to my OPA
01:41and I say, hey, you should be worth one.
01:44If I do that and I go into my application, we'll see that nothing will change.
01:49We'll still have that fade from zero to one.
01:52And you might be thinking, hey, but why?
01:53You know, I just changed it to in line 41.
01:56How is it that this animation is still happening?
01:58How is it that this opacity is still happening?
02:02Now, the reason is, is because once we use that property, that's it.
02:05It's already, we're not assigning the value of OPA into opacity.
02:10We're literally copying whatever's inside here and sending it to opacity.
02:15So if in line 40, OPA was worth zero, then that's it.
02:18We bake zero in here and it doesn't really matter what happens in line 41.
02:24Moreover than that, if we create another variable and I call it OPA2 or OPA2 and
02:29I set the value of OPA inside of it, and then I go into OPA and I say, hey,
02:34OPA, you're worth now one.
02:36And I go ahead and I send inside of here the value of OPA2.
02:41Let's think, what do you think the value would come through?
02:45Now, let's go through line by line and see what the value of OPA would be.
02:50So if in line 30, OPA is worth zero.
02:53Line 39, we're setting the value to OPA2 to the value that currently is of OPA,
02:59which is zero.
03:00Okay, so OPA is worth zero now.
03:02Now we're going ahead and updating OPA to be equal one.
03:06Question is, what is OPA2 equal to?
03:08And the answer is, it stays what it was.
03:10It doesn't change because we change OPA1.
03:12So literally, OPA2 would still equal zero,
03:16which means our animation would still happen, okay?
03:20So now that's how variables work.
03:23Contrary to that, if I go ahead here and I just, I'm going to delete this.
03:27And I'm going to create here a new variable and I'm just going to call it var.
03:31And I'm going to put inside of my anim inside of here anim.
03:35And I'm going to take this whole object right here.
03:37I'm going to copy it out, copy it out, put it right in here.
03:41Set my anim value right ahead in here.
03:44Okay, so now I have an object called anim and I'm setting it exactly in this spot.
03:49What do you think would happen if I go inside of my anim object and say, hey anim,
03:54from now on, your opacity equals one.
03:58From now on, your font size will equal 45 pixels, by the way,
04:07which is the default size that it was originally.
04:10What do you think will happen?
04:11Do you think that animation will take place no matter what and only,
04:15and this won't come into effect?
04:17It would only update our object, but it wouldn't update that animation taking
04:21place in our tween max.
04:22And if that's what you're thinking, then the answer is no.
04:25Actually, contrary to variables, objects, whenever you make a change to them,
04:29they immediately change in every other place where there is a reference to them.
04:33And literally, you see right now that because we did that,
04:36our animations literally didn't really do anything.
04:40Now, to be able to sort this out, the only way for us to sort this out
04:46is obviously not forcing this different value inside.
04:50And by the way, we could have forced also a value that was completely different.
04:53I could have said here the value of 100.
04:55And literally, as long as our animations didn't complete,
04:58it's going to force that value into it and change our structure of our animation
05:03that was going on.
05:04And if I look at this, I see here that I have your little bug.
05:07So I'm going to go into my console to see what is my bug.
05:09And I can see here that I have your OP2 is not defined.
05:13So I'm going to go here just really quickly and fix that,
05:15because it should be OPA, because OPA we deleted OPA2.
05:20And if I come back right in here, click on refresh,
05:23we'll see that the only thing animating right now is the size.
05:26And if we look at this, we'll see the size and the padding
05:29will be updating now, but not the actual opacity would not actually update.
05:38So if I, oh, silly me.
05:40Let me just set this to 45 and that to 45.
05:43So we'll actually see what I'm talking about.
05:44We'll see those two properties are not going to, did I save this?
05:50Okay, let's save it one more time.
05:53Because I actually put here a semicolon inside of my string here of my font size.
05:57So it was messing things out.
05:58So let me just save that and go back into the browser and click on refresh.
06:03Unfortunately, that type of bug doesn't even come up as a bug.
06:05And now you can see that only our padding is actually changing.
06:09So if I go ahead and I also undo the padding,
06:11we'll see that if I just type your anim and padding,
06:16and I just put the value of zero PX and I don't put a semicolon inside of the string,
06:21which I did by mistake, you'll see that actually, oopsie,
06:24you'll see that actually no animation is actually taking place.
06:27And literally, this process of what we've just done
06:31is exactly the big difference between objects and simple variables.
06:36So objects, any place we send them to is basically we're sending them a reference
06:42to this basically vault in many ways, an object is just like a closet.
06:47We're not duplicating the closet.
06:49We're just saying, hey, here's access to this closet we call the anim.
06:52But if I go ahead and I create a new variable called anim two,
06:57and I just set the value of anim inside of it,
07:00and I go ahead and I change the values of anim right here and initially,
07:06but instead of that I send anim two, the same applies
07:09because I'm not creating a new object.
07:11All I'm really doing is giving the same object another name.
07:28So if I make changes to this object, literally all I'm doing is making changes
07:33to the exact same object, both this one and this one.
07:35It's the same object.
07:37Contrary to that, when we're approaching elements directly,
07:40when we're approaching primitive values directly,
07:43which we said are numbers and strings and Boolean values,
07:46we're approaching them directly.
07:47They're not sitting inside of this imaginary bucket.
07:50But in this case, when we're approaching a bucket
07:52and we give that bucket a name,
07:55and we duplicate it by just putting it into another variable,
07:58we're not duplicating.
07:59All we're doing is just giving it another name.
08:01Thus, if we call it and send it other places,
08:04any change we make to that object will affect every place
08:07that references that specific object, even if it has a different name.
08:11So that is the biggest and most critical and most common bug
08:14most developers have when they're working in JavaScript.
08:17And in any other language that is object oriented really,
08:19is understanding that difference between the actual raw items
08:24that you could literally duplicate.
08:27I could create your 100 variables.
08:28There's no such thing as new number.
08:30I'm literally because it's not an object.
08:32It's just a free form number.
08:33So I could duplicate variables easily by just assigning that value.
08:37Once I assigned it, I'm literally assigning the value in it
08:40in that given point.
08:41Contrary to that, whenever I'm assigning objects,
08:44I'm not assigning the value of the object,
08:46but I'm assigning access to that object.
08:49And it could have a million names.
08:50It wouldn't change the fact that it's only one object.
08:53So that is the biggest difference between variables and numbers.
08:55It's a little bit complicated, but hopefully not too complicated.
08:59And let's continue in our next lesson.
09:01Because in our next lesson, we are going to go back
09:04to our original sample of how much time passed
09:06since I was born and start creating it all over.
09:09But this time, we'll leverage objects to make our life
09:14much more easy.
09:16All right.
