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

Adding methods to built in Objects

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:00What I want to do is I want to show you how prototype works
00:03in action by showing you how you could even
00:06add new prototype methods to various different built-in
00:14functions in JavaScript.
00:16So let's say if we take our, let's say, the string object.
00:20No, let's take something even more complex.
00:22Let's take the array.
00:24And what I want to do is I'm just going to,
00:27just for this example, I'm just going
00:29to delete everything here.
00:31We're just going to focus on prototyping for a second here.
00:34We're just going to leave the init function up.
00:36We're going to delete everything inside here.
00:38And I'm just going to come here and just add
00:40to the array, which is a built-in object.
00:45I'm going to go to its product prototype,
00:47and I want to add a new method.
00:48And the new method will be, it's going to be completely random.
00:55Let's say switch, switch, which will take in one,
01:00oh, he switches a reserved word.
01:01So switch elements, switch elements,
01:07which is going to be a function.
01:09It's going to take in two properties, which
01:12are going to be index 1, index 2.
01:16And really, all we want to do with this
01:18is basically approach our array element
01:21and switch the elements inside of it.
01:24Easy enough, right?
01:25So we're going to approach this, this index 1,
01:33and we're going to set its value to be this index 2.
01:40Now before we do that, we want to create a temporary variable.
01:43So I'm going to call it temp.
01:44I'm going to store the value inside of index 1
01:47into our temp.
01:48And now that we set the value of index 2 into index 1,
01:51all that's left for us is to go to index 2,
01:54put inside that temporary value right inside of there.
01:58And we've just created a new element, a new functionality
02:01of the array that wasn't there yet.
02:03Now every array that will be created,
02:05and any array that's in the applications,
02:06will have the function switch elements.
02:08Let's see if this really works.
02:10I'm going to create a new array.
02:12And this is the array I'm going to just put in your cat,
02:15dog, bird, bird, and then narf.
02:22All right, so let's test it out and see if it's true.
02:25So I'm going to first of all, I'm going to go to A,
02:28and I'm going to call that new function, which
02:31is called switch elements.
02:33And I'm going to say, let's switch
02:34the element that's in position 0 with 1, 2, 3,
02:38with the one that's in position 3.
02:39AKA, we should see narf first and cat last.
02:43And all that's left for us to do
02:46is to check and see if it's really true.
02:49Console, save that.
02:51We're going to go into our application,
02:53click on Refresh, no map, because we deleted everything
02:57related to maps.
02:58And we'll see that narf is coming first,
03:00our cat is coming last.
03:02We've just used our prototype.
03:03And imagine you have an application that already
03:05has a lot of built-in things.
03:07You can even add functions, methods to the string class.
03:10You could add quite a lot of functionality,
03:12dynamically changing objects that already exist,
03:15adding to the methods.
03:17Because it doesn't work in a class format,
03:19which other languages, you have a pre-built,
03:22this is the set defined of things
03:23that this object will be able to do.
03:25And then you create that new object.
03:27JavaScript doesn't work that way.
03:29It works with these linkages.
03:31And that's why it's called prototype, basically
03:33enabling us even much, much after we create many objects
03:37to add new properties to the whole genre,
03:39to the whole scope of these type of objects.
03:43So really in the heart of it, that's what prototypes are.
03:46And we've just seen how to extend a built-in object
03:49inside a JavaScript, which was basically we extended the array
03:53and added a new functionality to the array.
03:56And this could have been at any point of the game.
03:58Any point we could add new functionality
04:00to our every array, not only our array.
04:05On the other hand, if we would add the functionalities
04:07directly, such as we've seen earlier,
04:09and we would create a new function,
04:12this new function name is only going
04:14to be relevant to this specific object.
04:18Contrary to that, when we set it into the prototype,
04:21anything that is an array will have this method.
04:25And that's the big difference between working with the prototype
04:28versus not working within the prototype.

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!