Last active
August 8, 2021 12:49
-
-
Save AnkitMaheshwariIn/c6c831e75eeddef94f680cb3e53bed7e to your computer and use it in GitHub Desktop.
When to use the bind function?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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