温馨提示:本站仅提供公开网络链接索引服务,不存储、不篡改任何第三方内容,所有内容版权归原作者所有
AI智能索引来源:http://www.muo.com/call-apply-bind-javascript/
点击访问原文链接

How to Use Call, Apply, and Bind in JavaScript

How to Use Call, Apply, and Bind in JavaScript Menu Sign in now Close Trending Submenu Productivity Android Smart TVs Networking Windows 11 Entertainment PC & Mobile Submenu Windows2 Linux Android Apple Technical Submenu Tech Explained Security Software Submenu Productivity Internet Creative Screen Submenu Entertainment Streaming Home Submenu Smart Home Home News Sign in Newsletter Menu Follow Followed Like More Action Sign in now Productivity Android Smart TVs Networking Windows 11 Entertainment Close
How to Use Call, Apply, and Bind in JavaScript No attribution required -- Background image(Pexels) and logo(uxwing) Follow Followed Like Link copied to clipboard Add us on By  Unnati Bamania Published Dec 24, 2021, 12:16 PM EST Unnati is an enthusiastic full stack developer. She loves to build projects using various programming languages. In her free time, she loves to play the guitar and is a cooking enthusiast. Sign in to your MakeUseOf account

You may have come across various built-in functions like those for arrays and strings while practicing JavaScript. While you might use these more common methods in your day-to-day programming tasks, other methods can be used for special purposes.JavaScript's call(), apply(), and bind() are some such methods that have their special use-cases and are often used to test your JavaScript knowledge in coding interviews. Let's take a closer look at how you can use them.

1. call()

call() allows for a function or method belonging to one object to be assigned and called for a different object. You can refer to the values of the object by using the this keyword.

Check out this call() example.

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

The object obj has a function named getFullName() that prints the full name of the person. Now, if you want to use getFullName() with a different object that has different values, you can do so using the call() function.

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

You can also pass different arguments in the call() function along 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");
2. apply()

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

Here's an 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 Should Master Today​​​​​​

3. bind()

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

Here's 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 call(), apply(), and bind() functions are important when it comes to prepping for JavaScript interviews. There's a myriad of other core concepts in JavaScript that you should master to become the most efficient programmer you can be; why not start with array methods?

Close
Recommended 15 JavaScript Array Methods You Should Master Today The June Android security update is one of the biggest this year and most users haven't installed it yet I turned my Linux terminal into a walkie-talkie that no one can track I tried a two-panel file manager, and Windows Explorer has felt broken ever since Join Our Team Our Audience About Us Press & Events Media Coverage Contact Us Follow Us Advertising Careers Terms Privacy Policies MakeUseOf is part of the Valnet Publishing Group Copyright © 2026 Valnet Inc.

How to Use Call, Apply, and Bind in JavaScript,AI智能索引,全网链接索引,智能导航,网页索引

    JavaScript call(), apply(), and bind() stand a decent chance of showing up in your web dev interview. Are you prepared?