Underscore.js | _.bind() Function

The _.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: This function accepts three parameters as mentioned above and described below:

  • function: This parameter holds the function that need 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 bind a function to an object.

Example 1:



filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <script type="text/javascript" src=
    </script>
</head>
  
<body>
    <script type="text/javascript">
  
        var 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>

chevron_right


Output:

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <script type="text/javascript" src=
    </script>
</head>
  
<body>
    <script type="text/javascript">
        var obj = {
            Name: "GeeksforGeeks",
            Address: "Noida"
        };
  
        var fun = function (Geeks) {
            return 'Welcome to ' + this.Name
                + '\nAddress: ' + this.Address
        };
  
        fun = _.bind(fun, obj);
  
        console.log(fun());
    </script>
</body>
  
</html>

chevron_right


Output:

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.