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)
Call
What 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 _.
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)
clicked: undefined
(or similar — this is not view)
How It Works
No bindAll — method keeps dynamic this.
Free call does not set this to view.
this.label is wrong or missing.
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)
_.bind returns a new function; original method stays.
_.bindAll replaces view.click on the object.
Use bindAll when you’ll pass view.click directly to APIs.
Use bind when you want a separate wrapper only.
Compatibility
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 ChromeSupported · Desktop & Mobile
Full support
Mozilla FirefoxSupported · Desktop & Mobile
Full support
Apple SafariSupported · macOS & iOS
Full support
Microsoft EdgeSupported · Chromium
Full support
Internet ExplorerNo native support · Use a polyfill
Polyfill
OperaSupported · Modern versions
Full support
Samsung InternetSupported · Android
Full support
BunSupported · JavaScript runtime
Supported
DenoSupported · JavaScript runtime
Supported
Node.jsSupported · Server runtime
Supported
Android WebViewSupported · Modern WebView
Full support
_.bindAllUnderscore 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.