In this lecture, I'm going to describe a three builtin methods of function in JavaScript called apply
and Bind.
They are very simple to understand, but at the same time, there could be lots of situations in JavaScript
where using those methods is very efficient and flexible.
All right.
Let's go to the editor and demonstrate each of these methods using some examples.
I'm going to create object rights one person and create here some properties.
First name John.
Then last name, right, Smith, then I'm going to create a method for getting the full name right,
full name, then function.
And then return this start first name.
Plus.
Place space here and then plus this that last night.
OK, now I'm going to create function expression, which will define a person's age and the job, the
right var defined person, and then assign to that variable the function with the parameters, age and
job.
And then South Dakota braces right to counsel the clock.
Right, they start fullName plus these.
Then plus the age variable.
Then years old, and he is a.
And plus, your job.
OK, I'm going to call dysfunctional, right, define person and then inside the parentheses, let's
pass arguments for age and job rights, for example.
Twenty eight and developer.
So what do you think we'll get here?
I believe that you guess this will be definitely an error.
The reason is that this variable points to global object and on global level, fullName method doesn't
exist.
In order to work it, we need somehow to point this variable to the person object.
This is a moment when we can use call, apply and find methods.
Using those methods, we can define to which objects should refer this variable as first I'm going to
use call method.
Let's change this code a little bit.
So here we do not need to place parentheses because as we know, functions are objects in JavaScript
and therefore we can use dot notation in order to attach to it.
So write that call.
Now, inside the parentheses, we need to define an object as a parameter to which this variable should
refer.
So we need to write to your person.
We can already reload the page because call method.
Besides that, it defines this variable.
It also invokes the function at the same time.
So reload.
And here we have our text.
But instead of H and job parameters, we have undefined because we have not passed proper arguments
so we can define them here after person parameter write 28 and again developer.
Revote and we have John Smith is 28 years old and he is that developer.
All right, let's move on to the next method, which is applying.
It works in the exact same way as coal.
But the only difference is that we pass arguments as an right.
I mean, that arguments should be items of an array.
So.
Right.
Define person, then use apply method again, plus as a first parameter person object and then inside
the square brackets, write the arguments for each and job thirty and let's say designer.
So revolt and you see that we have John Smith is 30 years old and he's a designer.
Again, those two methods work in the exact same way.
And the only difference is that in case of a tie method argument, police should be always in a right.
Sometimes you may come across some situations in JavaScript where a method can be more useful for you
than call.
OK, the next and the last one is binde method.
Binde is a bit different from the previous ones.
When we used by the method we create a new Koppio function, then we can define to which object should
this variable refer.
And also we can pass the parameters.
The main difference between BINDE and previous Callon apply methods is that bind itself does not invoke
a function.
So we need to create a new variable and in that variable we have to store a function and attached to
it by the method.
So write variable get person which equals to define person, don't bind.
And inside the parentheses that's pass as parameter person object.
After that, we need to call the function using this newly created variable name, so right, get person
and inside the parentheses, I'm going to pass the arguments, for example, 19 as an H and student.
Reload and here we go, John Smith is 19 years old and he's a student, all right, when we use Binde
method, we can define parameters.
So right here after person 19 and student.
And then get rid of the arguments from get person.
Reload and you see that we have exact same result in some cases, we can define one of the parameters
inside the method as a permanent.
So let's live your age and write, for example, 35 and remove a student from here and then pass in
that person.
Different arguments, for example, musician.
Reload and you see that we have John Smith is 35 years old and he is a musician, so we can use this
age parameter as a default and on each invocation, we can just change the job arguments.
So invoke this function again and write as a job.
For example, driver reload and see that in both cases we have same age, 35, but with different jobs.
All right.
So I hope that everything is clear and you understand well how those methods work.
But before we finish, I'm going to write one more example.
Let's create another object and call it Niek, then create properties.
First name and last name, Doe.
Now I'm going to borrow fullName from personal object, I think that it's very simple now and you guess
how to do that.
So right in council, control the clock.
Person, that full name, dot com.
And here we need to pass as a parameter the second object that we have created, right, Nick Reloads
and we have Nick Doe in concept.
OK, I think that it's enough color and biometrics are very convenient and flexible tools and JavaScript
and lots of JavaScript libraries and frameworks use them.
I hope that you understood well how they work, and I believe that you can use those methods in your
code successfully.