Underscore.js _.bind() Function (original) (raw)
Last Updated : 25 Apr, 2025
Underscore.js _.bind() function is used to bind a function to an object. When the function is called, the value of this will be the object.
**Syntax:
_.bind(function, object, *arguments);
**Parameters:
- **function: This parameter holds the function that needs to be executed.
- **object: This parameter holds the object elements.
- **arguments: This parameter needs to add some symbols between the elements.
**Return Value:
It returns the value that binds a function to an object.
**Example 1: The below code example is the basic implementation of the _.bind() function in underscore.js.
html
<!DOCTYPE html>
<
html
>
<
head
>
`` <
script
type
=
"text/javascript"
src
=
`` </
script
>
</
head
>
<
body
>
`` <
script
type
=
"text/javascript"
>
`` let fun = function (Geeks) {
`` return 'Company Name : ' + this.Company
`` + '\nAddress : ' + this.Address
`` + '\nContact : ' + this.Contact
`` };
`` fun = _.bind(fun, {
`` Company: 'GeeksforGeeks',
`` Address: 'Noida',
`` Contact: '+91 9876543210'
`` });
`` console.log(fun());
`` </
script
>
</
body
>
</
html
>
**Output:
**Example 2: The below code is another implementation of the _.bind() method.
html
<!DOCTYPE html>
<
html
>
<
head
>
`` <
script
type
=
"text/javascript"
src
=
`` </
script
>
</
head
>
<
body
>
`` <
script
type
=
"text/javascript"
>
`` const obj = {
`` Name: "GeeksforGeeks",
`` Address: "Noida"
`` };
`` let fun = function (Geeks) {
`` return 'Welcome to ' + this.Name
`` + '\nAddress: ' + this.Address
`` };
`` fun = _.bind(fun, obj);
`` console.log(fun());
`` </
script
>
</
body
>
</
html
>
**Output:
Similar Reads
- Underscore.js What is Underscore.js? Underscore.js is a lightweight JavaScript library and not a complete framework that was written by Jeremy Ashkenas. It provides utility functions for a variety of use cases in our day-to-day common programming tasks. Underscore.js provides a lot of features that make our task 4 min read
- Underscore.js Introduction Underscore.js is a lightweight JavaScript library and not a complete framework that was written by Jeremy Ashkenas that provides utility functions for a variety of use cases in our day to day common programming tasks. With just under six kilobytes in size, this library basically provides us with a w 2 min read