Friday, July 24, 2026
HomeLanguagesJavascriptUnderscore.js _.bindAll() Function

Underscore.js _.bindAll() Function

BindAll() function in underscore.js is used to bind the number of methods on the object. Each method is given a method name. It is handy to work with the event handlers.

Syntax: 

_.bindAll(object, *methodNames)

Parameters:

  • Object: It is the object that contains different methods and functions to be bind.
  • methodNames: It is the names of methods present in  the object.

Return Value: It returns nothing.

Note: Please Link the underscore CDN before using this code directly in the browser through the code.

Example 1:

javascript




<!DOCTYPE html>
<html>
  <head>
    <script src = 
"https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js" >
    </script>
  </head>
  <body>
    <button id="button">button</button>
    <script type="text/javascript">
      var object={
          label  : 'neveropen',
          click: function(){ console.log(
                'clicked: ' + this.label); },
          hover: function(){ console.log(
               'hovering: ' + this.label); }
        };
        //using bindall function of underscorejs
        _.bindAll(object, 'click', 'hover');
        /* When the button is clicked,
           this.label will have the correct value.*/
        let btn=document.querySelector("#button");
        btn.addEventListener('click', object.click);
        btn.addEventListener('click', object.hover);
    </script>
  </body>
</html>


Output:

Example 2:

javascript




<!DOCTYPE html>
<html>
  <head>
    <script src = 
"https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js" >
    </script>
  </head>
  <body>
    <button id="button">button</button>
    <script type="text/javascript">
      var object={
          printNum:()=>{
            for(let i=0; i<5; i++)
                console.log(i+" neveropen")
          },
          func: function(){ console.log(
              'Function : ' + this.printNum); },
          output: function(){ "Output : "+this.printNum(); }
        };
        //using bindall function of underscorejs
        _.bindAll(object, 'func', 'output');
        // When the button is clicked
        let btn=document.querySelector("#button");
        btn.addEventListener('click', object.func);
        btn.addEventListener('click', object.output);
    </script>
  </body>
</html>


Output:

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

3 COMMENTS

Most Popular

Dominic
32520 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6902 POSTS0 COMMENTS
Nicole Veronica
12017 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12115 POSTS0 COMMENTS
Shaida Kate Naidoo
7023 POSTS0 COMMENTS
Ted Musemwa
7265 POSTS0 COMMENTS
Thapelo Manthata
6980 POSTS0 COMMENTS
Umr Jansen
6971 POSTS0 COMMENTS