Underscore.js _.bindAll() Method

Beginner
5 min read
Updated: Nov 2026
4 examples
Function

Definition

Binds a number of methods on the object, specified by methodNames, to be run in the context of that object whenever they are invoked. Handy for event handlers that would otherwise get a useless this (official Underscore docs). Stable Function helper — not deprecated. methodNames are required.

Pass the required arguments shown below. Underscore function helpers usually return a new function — check timing and call-count behavior carefully.

Syntax

Call _.bindAll. Start with this line.

Underscore
_.bindAll(object, *methodNames)
CallWhat it does
_.bindAll(object, *methodNames)Primary signature.
_.bindAll(...)Call with the arguments your case needs.

Load Underscore with import _ from "underscore" (or the UMD CDN build) so these helpers are available as _.

Library declarations
Underscore
_.bindAll(object, *methodNames)

Parameters

ParameterDescription
objectRequired. Owner of the methods
*methodNamesRequired. String names of methods to bind

Return Value

Return ValueDescription
*Same object (methods rewritten)

Underscore.js Examples

Example 1 — Official: Bind Handlers

Wire multiple view methods in one call.

Underscore
var buttonView = {
  label: "underscore",
  onClick: function () {
    return "clicked: " + this.label;
  },
  onHover: function () {
    return "hovering: " + this.label;
  }
};

_.bindAll(buttonView, "onClick", "onHover");

buttonView.onClick();
// → 'clicked: underscore'
buttonView.onHover();
// → 'hovering: underscore'
Try It Yourself

How It Works

  1. Two names listed — onClick and onHover.
  2. Each method is replaced with a bound version.
  3. this.label stays "underscore".
  4. Same idea as the docs’ jQuery .on('click', buttonView.onClick).

Example 2 — Detach After Binding

Prove a pulled-off method still has the right this.

Underscore
var view = {
  label: "save",
  onClick: function () {
    return "clicked: " + this.label;
  }
};

_.bindAll(view, "onClick");

var handler = view.onClick;  // like passing to .on(...)
handler();
// → 'clicked: save'
Try It Yourself

How It Works

  1. bindAll first — before you detach the method.
  2. handler is a free function reference.
  3. Call still uses view as this.
  4. This is the event-handler use case from the docs.

Example 3 — Without _.bindAll

Same detach without binding — this is wrong.

Underscore
var view = {
  label: "save",
  onClick: function () {
    return "clicked: " + this.label;
  }
};

var handler = view.onClick;
var result;
try {
  result = handler();
} catch (e) {
  result = "(error)";
}
// result is not "clicked: save"
// (this is not view)
Try It Yourself

How It Works

  1. No bindAll — method keeps dynamic this.
  2. Free call does not set this to view.
  3. this.label is wrong or missing.
  4. Fix: _.bindAll(view, 'onClick') before detaching.

Example 4 — _.bindAll vs _.bind

In-place many vs one returned function.

Underscore
var view = {
  label: "ok",
  click: function () { return this.label; }
};

// Single method, new function:
var one = _.bind(view.click, view);
one(); // → 'ok'  (view.click unchanged)

// Several methods, rewrite object:
_.bindAll(view, "click");
view.click(); // → 'ok'  (view.click is now bound)
Try It Yourself

How It Works

  1. _.bind returns a new function; original method stays.
  2. _.bindAll replaces view.click on the object.
  3. Use bindAll when you’ll pass view.click directly to APIs.
  4. Use bind when you want a separate wrapper only.

Browser & Runtime Support

_.bindAll ships with Underscore.js (1.x). It runs anywhere Underscore runs — modern browsers (CDN or bundle) and Node.js / Bun / Deno with npm install underscore.

✓ Stable · Underscore 1.x

_.bindAll()

Available in [email protected] (UMD / ESM). Method names required. Works in evergreen browsers and current Node LTS when Underscore is loaded.

100% With Underscore 1.x
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
_.bindAll Underscore 1.x+

Bottom line: Safe for production once Underscore is on the page or in your bundle. Not a native browser API — load Underscore first. Prefer a pinned CDN version (e.g. [email protected]).

Frequently Asked Questions

_.bindAll(object, *methodNames) rebinds the named methods so this is always the object — even when used as callbacks. Official pattern: _.bindAll(buttonView, 'onClick', 'onHover').
Yes. Unlike _.bind (which returns a new function), _.bindAll replaces the named methods on the object in place and returns that object.
Yes. You must pass at least one method name. Calling _.bindAll with no names throws — Underscore will not bind every method by accident.
_.bind wraps one function and returns a new one. _.bindAll updates several named methods on an object for event-handler use.
No. It only fixes this. Use _.bind or _.partial when you need partial application.
Detached handlers lose their object. Binding once with _.bindAll keeps this.label (and friends) correct when the handler runs.

Next: _.partial()

Pre-fill some arguments of a function without locking this.

_.partial() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful