bind()
bind() creates a new function with a fixed this value.
It does not call the function immediately.
It returns a new function that you can call later.
function greet() {
return `Hello, ${this.name}`;
}
const user = {
name: "Alice",
};
const greetUser = greet.bind(user);
console.log(greetUser()); // Hello, AliceThe important part:
greet.bind(user)This creates a new version of greet where this is always user.
Why Use bind()?
Use bind() when a function needs to keep a specific this value for later.
Common situations:
- passing methods as callbacks
- using timers
- event handlers
- creating reusable functions with preset context
- fixing lost
this
Basic Syntax
const boundFunction = originalFunction.bind(thisValue);Example:
function sayName() {
console.log(this.name);
}
const user = {
name: "Alice",
};
const boundSayName = sayName.bind(user);
boundSayName(); // AlicethisValue is the value you want this to be inside the function.
bind() Does Not Call the Function
This is a common beginner mistake.
function sayName() {
console.log(this.name);
}
const user = {
name: "Alice",
};
const bound = sayName.bind(user);Nothing is logged yet.
bind() returns a new function.
You still need to call it:
bound(); // AliceFixing Lost Method Context
const user = {
name: "Alice",
greet() {
console.log(`Hello, ${this.name}`);
},
};
const greet = user.greet;
greet(); // this is not userFix with bind():
const greet = user.greet.bind(user);
greet(); // Hello, AliceNow greet is a new function with this locked to user.
bind() With Timers
const user = {
name: "Alice",
greet() {
console.log(`Hello, ${this.name}`);
},
};
setTimeout(user.greet.bind(user), 1000);Without bind, user.greet would be passed as a plain callback and lose its context.
With bind, the callback keeps user as this.
bind() With Arguments
bind() can also preset arguments.
function multiply(a, b) {
return a * b;
}
const double = multiply.bind(null, 2);
console.log(double(5)); // 10Here:
multiply.bind(null, 2)creates a new function where the first argument is always 2.
This is called partial application.
null is used because multiply does not use this.
Binding Methods Once
Sometimes you create a bound method once and reuse it.
const counter = {
count: 0,
increment() {
this.count += 1;
console.log(this.count);
},
};
const incrementCounter = counter.increment.bind(counter);
incrementCounter(); // 1
incrementCounter(); // 2The bound function always uses counter as this.
Bound Functions Stay Bound
Once a function is bound, you cannot easily change its this with another bind.
function showName() {
console.log(this.name);
}
const alice = { name: "Alice" };
const ava = { name: "Ava" };
const showAlice = showName.bind(alice);
const tryShowAva = showAlice.bind(ava);
tryShowAva(); // AliceThe first binding wins.
This is useful to know when debugging.
bind() vs Wrapper Function
You can often solve the same problem with a wrapper function.
setTimeout(() => user.greet(), 1000);or with bind:
setTimeout(user.greet.bind(user), 1000);Both can work.
The wrapper is often more readable when the call is simple.
bind is useful when you need a reusable bound function.
bind() and Arrow Functions
bind() does not change an arrow function's this.
const user = {
name: "Alice",
};
const greet = () => {
console.log(this.name);
};
const boundGreet = greet.bind(user);
boundGreet(); // not AliceArrow functions use lexical this, so bind() cannot replace it.
Use regular functions or method syntax when you need binding.
Best Practices
Use bind() when you need a function to keep its this value:
const handler = user.greet.bind(user);Remember that bind() returns a new function:
const bound = fn.bind(context);
bound();Use wrapper functions when they are clearer:
setTimeout(() => user.greet(), 1000);Do not use bind() to fix arrow functions.
Avoid repeatedly creating bound functions inside hot loops unless necessary.
Common Mistakes
Mistake 1: Forgetting to Call the Bound Function
user.greet.bind(user); // creates a function, but does not call itCorrect:
const greet = user.greet.bind(user);
greet();Mistake 2: Binding to the Wrong Object
const greet = user.greet.bind(admin);Now this is admin, not user.
That may be intentional, but it should not be accidental.
Mistake 3: Trying to Bind an Arrow Function
const fn = () => this.name;
const bound = fn.bind(user);bind() cannot change an arrow function's lexical this.
Quick Check
What does this log?
function sayName() {
console.log(this.name);
}
const user = {
name: "Alice",
};
const bound = sayName.bind(user);
bound();It logs:
AliceDoes bind() call the function immediately?
No.
It returns a new function.
Summary
bind() creates a new function with fixed this.
bind()does not call the function immediately.- It is useful when passing methods as callbacks.
- It can fix lost context.
- It can also preset arguments.
- Bound functions keep their original binding.
bind()does not change arrow functionthis.- Use
bind()when you need a reusable function with a stable context.