Call (), apply () and bind () in JavaScript

Tram Ho

When learning and practicing with JavaScript, you are probably familiar with at least one of the three methods all (), bind () and apply (). We may not use these methods very often, but they are quite common in any JavaScript interview.

In JavaScript, functions are objects. Objects can include properties and methods. For almost any function, we can get these three methods.

But before we get started, let’s look at the following examples: When executing a function, this will be determined by the way a function is called (runtime binding).

  • In a method: mention the owner object.
  • In a function (sloppy mode): refers to the global object.
  • In a function (strict mode): undefined.

Now let’s get started …

call ()

To the definition of:

The call () method calls a function with an existing value and the arguments are given individually. In a nutshell, deciding what is going to be that – in the function when we call it.

Let’s see a basic example to understand this problem.

We have a function personIntro () trying to access the console of firstName and lastName. We have three outputs as follows:

  • If the call () method is not used, then this method will refer to the window object by default. The Window object will not have any properties of firstName or lastName. Therefore, the result is undefined and undefined.
  • If using call () and passing an object with properties is required, this is person . Therefore, the result will be Sanjeev Sharma .
  • Like the outputs above, just a further description of how call () works.

It is also possible to pass additional arguments in call () like so:

bind ()

To the definition of:

The bind () method creates a new function, and when called, its keyword is set to a given value, with a given string of arguments given any arguments passed when the function is called. called.

Sorry, too much information to process at once, so if you already understand call (), use that knowledge to understand bind ().

We create a function getPerson () again. Now there are two outputs:

  • Use call () and pass {state: ‘California’} (first argument) as arguments. The second argument will be person .
  • Trying to output will be the same as when using bind (). With the use of bind (), we can associate this value with one function and get another function in return. In the above case, we associate it with {state: ‘California’} and store the returned function in personFromCalifornia . Now when calling personFromCalifornia , just pass the argument to person . It will have a value like this.

Just call back a function similar to another argument person .

So what’s the difference between call () and bind () here?

  • the call () gets immediately invoked while bind () returns a function that can be invoked later.
  • call () takes additional argument but bind () doesn’t.
  • call () does not make a copy of the function, unlike bind ().

apply ()

To the definition of:

The apply () method calls a function with the given value, and the argument has been provided as an array (or an array-like object).

Basically exactly the same as call (), with only one small difference.

call () takes an individual argument, but apply () takes it as an array. That’s it.

Share the news now

Source : Viblo