| by Arround The Web | No comments

What is the Difference Between call() and bind()?

While appending various features to a web site or the web page, there can be a requirement to relate the functionalities based on a specific attribute. For instance, relating identical content in terms of code and functionalities at the same place. In such situations, the “call()” and “bind()” methods in JavaScript assist at the developer’s end.

This blog will discuss the difference between the call() and bind() methods in JavaScript.

What is the Dissimilarity Between call() and bind() Methods in JavaScript?

In JavaScript, the “call()” method invokes a method with an owner object as its argument. It binds “this” value to the function and executes the same function.

Syntax of call() Method in JavaScript

document = function.call(argN)

In the given syntax:

  • function” refers to the function that will be executed.
  • argN” corresponds to the arguments that need to be passed to the function.

Whereas the “bind()” method allows the object to fetch a method from another object. This method binds “this” value to the function and returns a new function.

Syntax of bind() Method in JavaScript

function.bind(thisArg[, arg1[, arg2[, ...]]])

In the above syntax:

  • thisArg” is the targeted function’s value.
  • Arg1” and “arg2” are the function’s arguments.

Example 1: How to Use call() Method in JavaScript?

In this example, the stated method can be utilized to invoke a function and associate a particular object with it:

<div id="demo"></div>

functionmyFunction() {
return"This is a simple " + this.firstName + " " + this.lastName + ".";
}
const element1 = {
 firstName:"web",
 lastName: "page"
}
const element2 = {
 firstName:"web",
 lastName: "site"
}
document.getElementById("demo").innerHTML = myFunction.call(element1);

In the above code:

  • Firstly, include a “<div>” element to contain the resultant output.
  • Now in the JavaScript code, define a function named “myFunction()”.
  • In its definition, associate object “this” with the “keys” of a particular elements.
  • After that, create two objects named “element1” and “element2”, respectively, in the form of “key-value” pairs.
  • In the next step, apply the “getElementById()” method to access the created “div”.
  • Also, associate the “call()” method with the stated function. As its parameter, pass the former object such that the function calls the particular object, and its properties are appended.
  • Lastly, utilize the “innerHTML” property to display the outcome in the “div”.

Output

In the above output, it can be seen that the functionalities of both the function and the object are appended.

Example 2: How to Use bind() Method in JavaScript?

In this example, the “bind()” method can be applied to bind a particular object with a specific object’s key specified as a function:

<div id="demo"></div>

const object1 = {
 mem1:"HTML",
 mem2: "CSS",
 myFunction: function() {
return"This is " + this.mem1 + " & " + this.mem2;
 }
}
const object2 = {
 mem1:"JavaScript",
 mem2: "jQuery",
}
let message = object1.myFunction.bind(object2);
document.getElementById("demo").innerHTML = message();

Here in this code lines:

  • Firstly, in HTML code, include the “<div>” element having the stated id named “demo”.
  • In the JS code, create an array of objects named “object1” having the stated values in the form of “key-value” pairs.
  • Note that “myFunction” refers to the object’s key, and the function is defined as its value.
  • In the function definition, refer to the object keys via “this” object.
  • After that, similarly, create another object having the stated properties.
  • Now, refer to the associated object’s key “myFunction” and attach the “bind()” method such that the particular object in the method’s parameter is integrated with the function.
  • Finally, apply the “getElementById()” method to fetch the “div” element and display the outcome via the “innerHTML” property.

Output

The above outcome displays that the desired requirement is fulfilled.

Conclusion

In JavaScript, the main difference between the call()” and “bind()” methods is that the call() method binds “this” value to the function and executes the function, whereas the “bind()” method binds “this” value to the function and returns a new function. This blog discussed the core differences between the call() and bind methods in JavaScript with the help of the examples.

Share Button

Source: linuxhint.com

Leave a Reply