Understanding the bind() Method in JavaScript
In previous blog posts, we explored functions in JavaScript, which are a type of object with properties and methods. We discussed the call and apply methods in the last post. In this post, we will delve into the bind method. bind() Syntax bind(thisArg) bind(thisArg, arg1) bind(thisArg, arg1, arg2, ..., argN) The bind() function creates a new bound function. Calling the bound function generally results in the execution of the function it wraps, which is also called the target function. MDN To better understand this, let's break it down. The key point is that the bind() method helps set the value of this in the target function to a specific object, ensuring it references the object we want. Let's use MDN's example: const module = { x: 42, getX: function () { return this.x; }, }; const unboundGetX = module.getX; console.log(unboundGetX()); // undefined A similar code like this works as we expected: const unboundGetX = module.getX(); console.log(unboundGetX()); // 42 This is because we're executing the module's getX function on the spot. What the first code is doing is only assigning the function to a variable, unboundGetX, without executing it --- just referencing it. const unboundGetX = module.getX; Then, when we call unboundGetX() somewhere in the code later, this inside unboundGetX no longer references module. Instead, this references the global object: window in non-strict mode, or undefined in strict mode. To ensure that this always refers to module, we can use the bind() method to create a new bound function: const unboundGetX = module.getX; const boundGetX = unboundGetX.bind(module); console.log(boundGetX()); //42 Properties of Bound Functions: length The length property tells the number of expected arguments. Example: function add(a, b, c) { return a + b + c; } console.log(add.length); // 3 (3 arguments: a, b, and c) const boundAdd = add.bind(null, 5); // a is defined as 5 console.log(boundAdd.length); // 2 (only needs `b` and `c` now) const anotherBoundAdd = add.bind(null, 5, 10); console.log(anotherBoundAdd.length); // 1 (only needs `c` now) name The name property tells the name of the target function with a "bound" prefix. Example: function add(a, b, c) { return a + b + c; } console.log(add.name); // add const boundAdd = add.bind(null, 5); console.log(boundAdd.name); // bound add Use Cases With setTimeout: When we pass an object method as a callback function, we lose this. The example below is based on the examples shared on The Modern JavaScript Tutorial. let user = { name: "John", sayHi() { console.log(`Hi, ${this.name}.`); } }; setTimeout(user.sayHi, 1000); // This won't work. We don't know "this" Here, we can use the bind() method. let boundSayHi = user.sayHi.bind(user); setTimeout(boundSayHi, 1000); // Hi, John. The Modern JavaScript Tutorial Understanding each property and method of functions in JavaScript, such as bind, provides a clearer comprehension of how and why they work. This knowledge is particularly useful when working on large-scale projects, where it's easy to get overwhelmed by numerous functions. Mastering these concepts will help in reading and creating more complex code effectively.

In previous blog posts, we explored functions in JavaScript, which are a type of object with properties and methods. We discussed the call
and apply
methods in the last post. In this post, we will delve into the bind
method.
bind()
Syntax
bind(thisArg)
bind(thisArg, arg1)
bind(thisArg, arg1, arg2, ..., argN)
The bind() function creates a new bound function. Calling the bound function generally results in the execution of the function it wraps, which is also called the target function.
MDN
To better understand this, let's break it down. The key point is that the bind()
method helps set the value of this
in the target function to a specific object, ensuring it references the object we want.
Let's use MDN's example:
const module = {
x: 42,
getX: function () {
return this.x;
},
};
const unboundGetX = module.getX;
console.log(unboundGetX()); // undefined
A similar code like this works as we expected:
const unboundGetX = module.getX();
console.log(unboundGetX()); // 42
This is because we're executing the module's getX
function on the spot.
What the first code is doing is only assigning the function to a variable, unboundGetX
, without executing it --- just referencing it.
const unboundGetX = module.getX;
Then, when we call unboundGetX()
somewhere in the code later, this
inside unboundGetX
no longer references module
. Instead, this
references the global object: window
in non-strict mode, or undefined
in strict mode.
To ensure that this
always refers to module
, we can use the bind()
method to create a new bound function:
const unboundGetX = module.getX;
const boundGetX = unboundGetX.bind(module);
console.log(boundGetX()); //42
Properties of Bound Functions:
length
The length
property tells the number of expected arguments.
Example:
function add(a, b, c) {
return a + b + c;
}
console.log(add.length); // 3 (3 arguments: a, b, and c)
const boundAdd = add.bind(null, 5); // a is defined as 5
console.log(boundAdd.length); // 2 (only needs `b` and `c` now)
const anotherBoundAdd = add.bind(null, 5, 10);
console.log(anotherBoundAdd.length); // 1 (only needs `c` now)
name
The name
property tells the name of the target function with a "bound" prefix.
Example:
function add(a, b, c) {
return a + b + c;
}
console.log(add.name); // add
const boundAdd = add.bind(null, 5);
console.log(boundAdd.name); // bound add
Use Cases
With setTimeout:
When we pass an object method as a callback function, we lose this
.
The example below is based on the examples shared on The Modern JavaScript Tutorial.
let user = {
name: "John",
sayHi() {
console.log(`Hi, ${this.name}.`);
}
};
setTimeout(user.sayHi, 1000); // This won't work. We don't know "this"
Here, we can use the bind() method.
let boundSayHi = user.sayHi.bind(user);
setTimeout(boundSayHi, 1000); // Hi, John.
The Modern JavaScript Tutorial
Understanding each property and method of functions in JavaScript, such as bind
, provides a clearer comprehension of how and why they work. This knowledge is particularly useful when working on large-scale projects, where it's easy to get overwhelmed by numerous functions. Mastering these concepts will help in reading and creating more complex code effectively.