00:00All right, so let's continue in our lesson.
00:02So far, we've seen how to use the prototype
00:04to add functionalities to existing classes.
00:07But again, JavaScript has no such thing as classes.
00:11They're always called objects.
00:12There's only objects in JavaScript.
00:15Functions and objects, they're almost the same thing,
00:17if not the same exact same thing.
00:20All right, so our next step in our chain
00:22is we want to create our own unique class.
00:24We've started doing that, our own unique object.
00:27It's very confusing, these terminologies.
00:29Our own unique object by creating its constructor.
00:32What our real goal is gonna be is to extend
00:35the behaviors of the array, so we can take the array
00:37and take all the features of the array,
00:39all the functions and functionalities of the array,
00:42and extend them over to another new object
00:46that will be able to have other functionalities.
00:48And this is exactly the concept of inheritance.
00:52And in JavaScript, it's not exactly inheritance.
00:55It's kind of like this key chain
00:56where your array is connected to a prototype,
00:59which your prototype is then connected to another object,
01:02and it's a chain that could be broken at any point
01:05and reconnected with other objects at any point.
01:08Contrary to many other languages
01:10where you have a strict defined class hierarchy,
01:13in JavaScript, it's functions.
01:15And they're just functions that are connected
01:17to each other, and thus you could disconnect them
01:20and reconnect them or create new links at any point,
01:23any new chain links at any point.
01:25And that's exactly what we're gonna do right now.
01:27The way you connect new chain keys
01:29is using the prototype as well.
01:31So let's see how that works.
01:33So what I wanna do is I wanna create a new array X.
01:35So I'm just gonna call a new constructor,
01:38which is basically just creating an array,
01:41and I'm gonna call it array X,
01:42or let's call it X, X, T, X, X, array X,
01:47for array extra.
01:49All right, so our array extra,
01:50before we even extend it into the other array,
01:53let's define what functionalities we wanna add
01:55to this magnificently extra array,
01:58that only whenever we want that extra functionality,
02:01we'll create that new array extra.
02:03Contrary to what we did earlier in line 11 through 15,
02:06where we added new functionality to an existing class
02:09without creating a new class.
02:11And I'm using the word class again,
02:13not because there are classes,
02:14it's just easier if you're coming from languages
02:17that do have classes.
02:19In our case, we're literally just adding more functions
02:22directly into the prototype of objects, all right,
02:26but it just sounds better by saying class.
02:28And by the way, the best tip I could give you
02:30while you're learning this,
02:32do not go to formal documentation
02:34because it's just gonna confuse you more,
02:36just experience with it and run with me
02:38and see how we're gonna build applications with it.
02:41Once you know how to build it,
02:42then go back into the formal documentations
02:44and then they'll make a lot more sense
02:46if you're completely new to prototyping, all right?
02:49So let's see this in action.
02:50So we created our constructor.
02:51Our next step is I wanna create a new property.
02:54So I'm gonna go to that prototype
02:55and let's just create a new function.
02:57We're gonna call it maybe find all,
02:59which is gonna be a function,
03:02which will take a key
03:03and then it's gonna search through our array
03:05to find the different keys that are possible.
03:08Our next is gonna be an array prototype
03:11and I'm not gonna put anything in them for now.
03:13It's gonna be change by key, all right?
03:19So by value.
03:24It's not key, that would be value, right?
03:26Because we wanna search for the value
03:28without knowing what the key is.
03:30All right, so change by value
03:31and really the idea of both functions
03:33is gonna be that we're gonna put here
03:34some sort of a value, search for the value
03:37and then change the content accordingly
03:40to whatever new value we want it to be,
03:42such as value two.
03:46All right, so we have two new functions
03:48that we wanna build into this new array extension
03:51and we'll build them out.
03:52But before we do that,
03:53we wanna link between our array,
03:55our regular array and our array extension
03:58to make sure that all the functionalities of the array
04:01will be automatically inherited
04:03into the array extension as well.
04:08All right, so to do that,
04:09what we wanna do is we wanna go
04:10right after we define our constructor
04:12and just before we start putting our prototypes,
04:15we wanna type in our array.prototype
04:18and instead of assigning to it a value,
04:20we wanna assign to it a new object.
04:23And again, because there's no such thing as classes,
04:26we're not gonna connect to it a class,
04:28we're not gonna connect to it the array
04:29because all that is is the function.
04:31We wanna turn that function into that array object.
04:34To do that, we just type new array,
04:37which then creates a full object.
04:39And now a full object is the prototype of our new array.
04:43Oh, not array, actually all this array here
04:46is supposed to be array extend,
04:48which probably is a bit confusing.
04:51So let's just fix that really, really quickly
04:52and then I'll say it again.
04:53So obviously we're adding new functions,
04:56new methods to our array X.
04:58And now while we're doing that,
05:02and I'm sorry, because that probably was very, very confusing.
05:04So let me just delete this line for a second,
05:08make sure that it's really clear.
05:10So we created a constructor array extend, array ext.
05:14We added two functions, two methods.
05:16One is gonna be called findAll,
05:17which will add the functionality as soon as we could.
05:21We created another one, which is changeByValue,
05:22which enables us to change the values inside of an array.
05:26Based on one value, we search for one value,
05:28replace it for another value.
05:29And now we're ready really to link it with that array.
05:35So again, so we're gonna array ext for our array,
05:38then prototype, that prototype.
05:40And then we're gonna connect it to a new array,
05:42basically building a new array right into our prototype.
05:46All right, now that we've done that,
05:48we have a new array extend.
05:50And our new array extend will extend
05:52the functionalities of our array.
05:54Now to test this out quite easily,
05:56what we could do is,
05:57and I'm gonna deliberately make it
05:59a little bit more complex,
06:00is instead of that shortcut building, right,
06:03you could do the long-handed,
06:06and really the shorthand only works for object and array.
06:09So I'm gonna just reform it into the regular way
06:12that we build objects by saying new,
06:14and then the name of the object that we wanna create.
06:17In our case, we wanna now build the array extend.
06:19So instead of that,
06:20we're gonna replace the name from new array to new array extend.
06:24We can't use shortcuts.
06:26Now we will have a problem.
06:28Our array will be created,
06:30but as we could see in our console log,
06:33and I'm gonna delete this function
06:35because it's not needed.
06:37When we run this, let's see,
06:39first of all, we're not gonna get any error
06:40cause switch elements exist inside of our array
06:42because we're inheriting it from the array itself.
06:45But let's see what value we come back
06:47and we're not gonna get the value that we would hope.
06:49We're not gonna get all this cat, dog, bird, and gnarf
06:52because, and we'll explain this right away,
06:54let's see why we're,
06:55let's see first that we're not really,
06:57we really are not gonna get it.
06:58So first of all, you could see that the values
07:00that I'm getting back are undefined, undefined, undefined,
07:03which is quite interesting.
07:04We're getting that in line five.
07:07So if we look at line five,
07:08we see that we're logging
07:10and we're getting this very, very weird behavior.
07:13Now, the reason why we're getting this weird behavior
07:16is very simple.
07:17The reason is, is because when we created our prototype,
07:20when we created our new array,
07:22we already set the values
07:23that are actually gonna go inside of our array.
07:26We set them to be nothing.
07:28And thus we kind of confused the process here
07:31and made it a little bit more tricky.
07:33What we really wanna do is we wanna go to this array extend
07:36and somehow add these functionalities
07:38that are now missing from our new array
07:40that we've just created.
07:43To do that, we could very easily
07:44and very quickly use a property of every function
07:47and every function has something that's called arguments.
07:51And arguments is basically,
07:54the most simple way of saying it,
07:56it's a stripped down array
07:58of all the properties of that function.
08:00Even though we didn't set formally any amount of arguments,
08:04we will have an arguments object
08:06and it will send in everything
08:07that came in all the arguments.
08:09So in this case, it will be these four arguments
08:12in this minified array
08:13that doesn't have all the different methods of an array,
08:15but it's gonna have that indexing of arrays
08:18as arrays do have.
08:20So if we run this and if we look at this in our side
08:23inside of our browser and refresh again
08:25and look at the value that comes back
08:27and first of all, we have your small little error.
08:30So let's fix it before we move on.
08:36Arguments console dot log,
08:41that looks all good to me.
08:43Let's refresh this one more time.
08:46And here we go.
08:47So first of all, we're already saying that in line 17,
08:49we are getting our elements,
08:51we are getting our cat, dog, bird, narf.
08:54The last thing that really we have left to do
08:56is to make sure that we're actually pushing those values
08:58into our array as the last thing we do
09:00before we complete our application.
09:03So to do that, what I'm gonna do is,
09:05what we wanna do is basically go into our element
09:09and there's a lot of ways of doing this.
09:10One thing that we could do
09:11is we could loop through our arguments array
09:15by just creating a for var i equals zero,
09:19i is smaller than arguments dot length,
09:23which is gonna give us the length of our elements,
09:28i plus plus,
09:30and then we could just manually assign to this
09:35and the value itself, i,
09:37and just directly assign inside of it the actual value,
09:40which was the arguments i,
09:42and we're basically just manually overriding the functionality,
09:45making sure that we have our elements inside,
09:48we could delete the sum call.
09:51And basically what we're doing here in,
09:53oh, we have a little syntax error,
09:56probably over-deleted, yep, that's what I did.
09:59All right, and basically what we're doing
10:01is we're basically overriding our array x
10:05to have these new arguments that are coming in
10:07just in the exact same way
10:09as the new array would have done it
10:11if we would create a new array in the traditional way.
10:14So if we run our application one more time,
10:15hopefully we'll see inside of our array
10:18that now we're getting our elements
10:19and they're inside of our array,
10:21not only they're inside of our array,
10:22we could very easily and very quickly
10:27access it exactly the same way
10:29as we accessed arrays earlier,
10:31and we should be getting the exact same functionality,
10:33so in our case, we should be getting a bird
10:35in our result, and we do.
10:38All right, so we've seen quite a lot here,
10:40so let's recap this really, really quickly.
10:43In our first video, we created a new array
10:46and extended that array's functionality
10:48by adding into its prototype another method.
10:51Our next step after that,
10:52we decided that we wanna create a completely new array
10:55and call a new variation of the array
10:57and call it array extend.
10:59In the array extend, we've added,
11:01we connected that array to the original array,
11:04making sure that we're taking all the built-in functionalities
11:07of the array, and we're inheriting it
11:10into our array extend using a key chain.
11:12So again, if my hands are up here,
11:14we're connecting a key chain,
11:15we're connecting our new array with the old one,
11:18and it's really just like, in many ways,
11:20it's just like a paper that's see-through
11:24and another paper that's see-through,
11:26and if I don't have a property here,
11:28if I don't have the property that's called switchElements,
11:30then it's gonna go to my prototype, check if it does.
11:33If that array doesn't, it's gonna go to its prototype
11:36and so on and so forth until it comes back with nothing
11:39or until it comes back with an element,
11:41with a method that does exist.
11:43So it doesn't stay, it's not that it's duplicating
11:45everything inside of the array into our element,
11:49it's duplicating it inside of the prototype,
11:51and now we're basically traveling through the prototypes
11:54behind the scene until we reach the method
11:56that we're looking for,
11:57or until we reach the property that we're looking for.
12:00We then created two functions, the findAll
12:03and the changeByValue,
12:05and we connected them to our new array extend prototype,
12:08basically extending our array again
12:11only, extending it only if it's a sun
12:14or a prototype of our array extend,
12:17because we didn't connect it directly to the array
12:20into the array's prototype as we did in line nine, all right?
12:24So basically, it's a lot, we did a lot.
12:27In the next video, in the next step,
12:28we'll continue in working on the findAll,
12:31we'll continue working on the changeByValue,
12:33and we're gonna be doing,
12:34and then we'll move into the Google Maps
12:35and see how we could do this also with Google Maps
12:37and extend different functionalities of Google Maps itself.