02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

Creating and Working with Properties

The best way to see how prototypes work are by seeing them and interacting with them. To do that we will revisit a variable we created in an earlier video. We will now create the variable again using prototypes. We then will look at the two as now we got two variables with the exact same name. So how can this work and how is it that its not a conflict. This is exactly the point where we go back to the meaning of this and how it works and relates to variables that are defined in the constructor verses ones that are created using the prototype.

One of the most important things we will learn here is the importance of scope. You will see that over and over we mention and talk about the scope. This is because the most important concept in OOP is scope once you get it and really understand how it works in one language you will understand it in any language.

When we refer to something using the this command we are actually refereeing directly to the object itself and thus when we create a variable that is tied directly to the this we are actually creating a local variable or “private” variable that only is available to this object while creating variables using the prototype we are actually adding the variable to every object of that type and and prototype that inherits from this one. There are a few other goodies that we talk about in this section so don't skip it, very critical.

Video Transcript

00:00All right, so let's talk about prototype and really the best way to talk about it is to just go and start doing it
00:05And again, we're in a new file called
00:07004 dot prototype dot JS and don't forget to update your HTML. So I'm gonna update my
00:14Variable ver to be called to be version 1 dot 003
00:18But before I play our application
00:20I want to uncomment line 11 where I commented out that we had our variable which was in version 1 dot 002
00:27Now we have a conflict. We have a variable that's in line 11 called ver and we have a variable in line 25
00:32Which is called ver as well now
00:35It's not really a conflict and the reason is because everything in JavaScript works with inheritance
00:40And it's basically even a better name would be a keychain. So
00:44Are you have the object itself?
00:46Which is what's when we refer to something directly when we refer to the this we're actually referring to the object itself
00:53Well when we're referring to the prototype, we're kind of referring to the blueprint. We're referring to what we wish it to be now
01:00basically, the line of order is we first
01:04JavaScript will first define all the prototype elements and only after that it will start overriding whatever it needs to base in what we
01:12Put directly on to our object
01:14Thus if we go into our Safari right now and refresh it and look at our trace. We'll see that we're at version
01:211 0 0 3
01:23but if we go back into our code and actually save the changes that we've just made and
01:29Go back again
01:30We'll see that our version will be 002 making our variable that we created inside of our object
01:36Override the variable of the prototype and that's really in the heart of what a prototype is and it's way beyond that and really in
01:44The next couple of videos that we're going to see really the heart of why we want to work with prototypes and how prototypes work
01:50the idea of a prototype is if you've worked with object-oriented programming before is
01:56Talking directly to the blueprint or talking directly to the class or talking talking directly to the thing that's going to create our object
02:04Contrary to that when we refer to something with this we're referring directly to the object that has been created thus in line
02:1211 we're actually adding in this variable to our variable or in many ways
02:17Updating the value from 1.00 3 to 1.00 2
02:21All right now there it's always true
02:25It's always whatever is nearest to the object is what takes into effect
02:29And that's really all you really need to know at this stage with prototype now

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!