bind()
noun
say it: “BYND” /baɪnd/
A method every function has that makes a copy of the function with this fixed to the object you give it, so the copy keeps working when it's passed around.
What is bind() in JavaScript?
bind() — A method every function has. It returns a copy of the function whose this is fixed to the object you pass, so a method still knows its object when it's handed over as a callback.
const comet = {
name: 'Comet',
fire() {
return `${this.name} fires!`;
},
};
const fireButton = comet.fire.bind(comet);
console.log(fireButton()); // Comet fires!
In a sentence
- “I used bind to fix this to the counter before handing its add method to the button.”
Level: Journeyman · More JavaScript terms at this level
Updated