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

Call and Apply

Functions are objects. There probably the most primitive type in JavaScript. To show case this we will create an example working with scope. Don't worry yet if you still are having issues with understanding scope. Its one of these core features of a language that just suddenly click and when it does it all sits in place. We will delve more deeply in this course into the hearts of all programing languages – the scope and knowing always where you are or part of what object you are. By the end of the first session you will have a basic understanding of how scope works and how its important to always asses where you are.

As every function is actually on its own a type of object we can trigger two advanced functions called 'call' and 'apply'. Both functions are used to control and change the scope of the function. To understand what that means we first will understand what scope is and then we will see it in action in both these function object methods.

Video Transcript

00:00So again, I've updated our file, but I'm not gonna say this anymore
00:03I'm gonna update it once in a while that way you'll have the latest version that you could jump in whenever you want to I
00:08Want to continue in talking about functions take a little break from object oriented
00:13But not really because what I want to show you is that functions are objects
00:18And they're probably the most primitive type of object in JavaScript
00:22Excuse the planes that are flying above me so to showcase this example
00:27And it will be kind of like another wave of us to talk more about scope
00:31And don't worry if the concept of scope is not a hundred percent in there
00:36Especially not the topic that we're going to be talking about now because this is really advanced stuff
00:40Now once you get the stuff really get the stuff you're you're golden you'll be fantastic
00:46JavaScript object-oriented JavaScript developers, so let's get let's get rolling with it. We'll introduce to you this concept of
00:54Scope more deeply and we'll revisit it again in another hour or two once we've built up our skill set
01:01All right, so I'm going to create a function, and I'm going to call it. What's my scope?
01:07This what's my scope function will take in two parameters. It's going to take in a copy one copy two and
01:13All my all what's my scope going to do is it's just going to call the console
01:18Log and it's going to output the copy one. I'm going to put inside the this in between copy two and
01:26That's all it's really going to do now for us to test it out
01:30All we want to do is Jenna. We're going to call what's my what's my scope?
01:33And we're going to put inside the first parameter the this value is
01:39is
01:41And we'll put the second parameter is let's say
01:45But it can be changed
01:48All right, so we created here two pieces of text the text is being sent as two parameters
01:53Copy one copy two and we're going to just enter the console just in the middle there add to this now
01:58We've seen this already and we saw that the this inside of a just a general function is the window
02:04If we go into our Safari or any browser and look into our console
02:08We won't have any surprise when we see that our window will be outputted there in the middle now
02:13If we wanted to change the scope that's where we start finding out how functions are actually objects
02:19And I want to show you and really what we're going to learn in this video mainly are two different
02:23Functions that belong to any function so every function has different functions that you could operate for them
02:29But let's see it in action before because it sounds crazy that every a function has a function very very very very interesting
02:37All right, so instead of calling what's in my scoop this way. I really want to change the scope
02:41What's my scope not scoop?
02:43What I really want to do is I want to call the scope again
02:46But this time around I really want the scope to be our student map that we've just created
02:50To do that instead of calling directly the function. I could type a dot right after what's my scope and then type call
02:58Just by doing that
02:59I'm kind of taking an extra step before I'm calling the function and my first parameter that I have to put in is the
03:06Scope itself what I'm actually doing here, and I'm not changing anything in the signature of my function itself
03:11What I'm doing is I'm telling the function what its scope what the scope of that function is going to be
03:17Thus by me telling it that it's going to be the student map if we go and refresh the application just by calling this call
03:24You're going to see that as we refresh it and go right back in there
03:28We'll see that now our student map is our scope our this is now the student map and that is because we used that call
03:36Function fault call method. Let's call it because it belongs to our function now, right?
03:40So there's this call method that belongs to our function
03:43There's a few other ones that I want to cover them and I know that right now even though you see this and you probably
03:48Get that it works, and you understand why it works. You probably don't really get what is the value of actually using it
03:55But you will later on in this course and even in this video
03:58We'll do a really quick example of showing you a real-world scenario where changing the scope becomes or not necessarily
04:05In our case we're not going in this video
04:07We're not going to see why changing the scope is such a good idea
04:10But we're going to see how using one of these methods call or apply is going to be useful
04:15So the next one is I'm going to show you another method of every function every function has it and I'm going to call it apply
04:21Now apply works in the exact same way as call does the only difference is it only takes two parameters in
04:28While call will take as many parameters as you need depending on this on the function itself
04:32The apply method only takes the first which is the scope and the second one which is an array
04:37So all I have to do is take all the parameters that I've set up here and convert them into an array
04:42All right now if I run my application again using the apply function
04:46We're going to see the exact same functionality and all the differences is that it's two different ways of doing the exact same thing
04:52So if we go again into our application refresh again
04:56We will find out that we're still getting our student map as our new scope and by the way
05:00Just one more thing before we move on is you could also define the scope to be nothing null undefined
05:07and if I set it to be undefined and I go back into my application and I
05:16Oops, and I go back into my application and I refresh it. We're going to see that my scope now
05:22Whoop did not change because I did not refresh it now
05:26It's back to the window because our scope basically reset itself to what it actually is because we set it to be nothing
05:32All right
05:33So back into our application because it's not really saying that you want to do we would never really want to call a function and said
05:38It's not itself not to have a scope
05:40We'll actually find out that sometimes that actually happens in JavaScript and we try to avoid it or when our scope gets confused
05:47but in our case, we always want to have a scope and
05:50our next
05:51example that we're going to do right now is just to showcase where apply becomes handy before we move on and continue working back into our
05:58Object oriented topic and continuing exploring the different properties that functions have
06:03All right, so I'm going to comment this out as well, and I just want to show you one quick sample now
06:08Imagine that we have an array
06:09I'm just going to call it a for our example and this array has a various various amount of numbers and
06:15They're quite a lot of them and we want to figure out. What is the actual smallest number in our
06:22Equation now imagine that it's a really long array now
06:25if you already are familiar with the math and math package inside of JavaScript the math package comes with an option to be able to
06:33get a
06:36Floor or lower
06:38What should we call it max or min?
06:40So you have the max and you have the min which both accept an unlimited amount of variables so I could say you're a hundred
06:47Whatever whatever amount that I put here, and then I would get that minimum amount
06:51So I could console log this and I will get that value back now
06:58My only problem is is now that I have an array and sometimes you will you'll have an array, but you will not have an actual
07:05You know you have no capability of actually listing out all these properties one by one
07:09So let's just see the first that this works
07:11We should be expecting to get minus five in our application
07:14So if we run this one more time and go to our browser once it finishes loading
07:19We should be getting and here we go. We got our minus five
07:22So basically we got our lowest value by just setting all these parameters one after each other now
07:28Our only problem is is we can't really push in our array this way because our rate just doesn't work in this way
07:34Lucky for us now. We know how the apply function works and because we know how the apply method works excuse me
07:40We could actually call even the built-in classes even the built-in functions. Let me just comment this one out and
07:48I could call in the built-in function. So I'm gonna just start with a console dot log and
07:54I'm gonna call the math dot min dot apply
07:59Open my my round bracket. I'm gonna set my round
08:04Yeah bracket gonna send in our
08:08Scope which is gonna be just the same scope so it doesn't really matter
08:12So I'm gonna just set this to this and I'm gonna move on to the next parameter
08:16Which is gonna be our array and that there you go. So we just set our array
08:20We can now close our log as well save this up go to our browser and check to see if we're really gonna get minus a thousand
08:27So I'm just gonna go right here refresh it and we should be getting minus a thousand and so we do so
08:32You can see very very vividly how apply and a call might become useful
08:38And you just have to keep your mind open about it
08:40But really our main topic here is about exploring deeper into the function
08:44Because the function is the heart of object-oriented programming in JavaScript
08:48Thus if you really really know really really well how to work with a function, you know object-oriented programming in JavaScript

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!