Skip to content

Instantly share code, notes, and snippets.

@AnkitMaheshwariIn
Last active August 8, 2021 12:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AnkitMaheshwariIn/c6c831e75eeddef94f680cb3e53bed7e to your computer and use it in GitHub Desktop.
Save AnkitMaheshwariIn/c6c831e75eeddef94f680cb3e53bed7e to your computer and use it in GitHub Desktop.
When to use the bind function?
function fullName() {
return "Hello, this is " + this.first + " " + this.last;
}
console.log(fullName()); // Output: Hello, this is undefined undefined
// create a person object and pass its values to the fullName function
var person = {first: "Foo", last: "Bar"};
// pass a person object to the fullName function using bind
// bind object to function -> fullName.bind(person)
// self call a function -> fullName.bind(person)()
console.log(fullName.bind(person)()); // Output: Hello, this is Foo Bar
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment