How to use call, app, and bind in JavaScript

0


JavaScript call (), apply (), and bind () have a good chance of showing up in your web development interview. Are you ready?



Laptop, coffee mug and JavaScript logo

You may have encountered various built-in functions such as those for arrays and strings while practicing JavaScript. While you can use these more common methods in your daily programming tasks, other methods can be used for special purposes.

Javascript to call(), apply(), and bind () are just a few of these methods which have their particular use cases and are often used to test your JavaScript knowledge during coding interviews. Let’s take a closer look at how you can use them.

1. call ()

to call() allows you to assign and call a function or a method belonging to an object for a different object. You can refer to the values ​​of the object using the this keyword.

Check it out to call() Example.

let obj = {
name:"John",
surname:"Doe",
getFullName: function(){
console.log(this.name+" "+this.surname);
}
}
obj.getFullName();

The object obj has a named function getFullName () which prints the person’s full name. Now if you want to use getFullName () with a different object which has different values ​​you can do this using the to call() a function.

const obj2 = {
name:"Jane",
surname:"Doe"
}
obj.getFullName.call(obj2);

You can also pass different arguments in the to call() work with the object.

let obj = {
name:"John",
surname:"Doe",
getFullName: function(age, gender){
console.log(this.name+" "+this.surname+" "+age+" "+gender);
}
obj.getFullName.call(obj2, 21, "female");

VIDEO OF THE DAY MAKEUSE

2.apply ()

the apply() the function works in a similar way to to call() a function. The only difference between the to call() and apply() The function is that you can pass multiple parameters in the array and use them.

here is a apply() Example:

const obj = {
name:"John",
surname:"Doe",
getFullName: function(age, gender){
console.log(this.name+" "+this.surname+" "+age+" "+gender);
}
}
obj.getFullName.apply(obj2, [21, "female"]);

Related: JavaScript Array Methods You Need To Master Today

3.link ()

bind () returns the exact copy of a function and binds it to an object. This method is used to link and keep a copy of a method and use it later. You can use the function whenever you want by invoking it.

here is a bind () Example:

const obj = {
name:"John",
surname:"Jane",
getFullName: function(){
console.log(this.name+this.surname);
}
}
const obj2 = {
name:"Jane",
surname:"Doe"
}
let func = obj.getFullName.bind(obj2);
func();

Never stop learning JavaScript

the to call(), apply(), and bind () Functions are important when it comes to preparing for JavaScript interviews. There are a myriad of other basic JavaScript concepts that you must master in order to be the most effective programmer possible; why not start with the array methods?


javascript-array-methods

15 JavaScript array methods you should master today

Want to understand JavaScript tables but can’t seem to master them? See our sample JavaScript tables for tips.

Read more


About the Author


Share.

About Author

Comments are closed.