auto-bind

Automatically bind class methods to their instance, including inherited ones

Library
npm
v5.0.1
462stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
36/100Needs Attention
Development Activity0
Maintenance20
Community44
Maturity60
Momentum20

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
61/100Good
Architecture55
Code Quality80
Innovation65
Learning Curve45

auto-bind is a tiny, zero-dependency utility from Sindre Sorhus that binds every method on a class instance to this in a single call, so you can freely destructure methods off an instance or pass them as callbacks without losing their context. It walks the full prototype chain — including inherited methods — and supports include/exclude filters (strings or RegExp) for binding only a chosen subset of methods.

A dedicated auto-bind/react entry point pre-configures the exclusion list for React’s class-component lifecycle methods (componentDidMount, render, shouldComponentUpdate, and others), making it a drop-in replacement for manually binding handlers in a constructor.

What You Get

  • A single default export function that binds every own and inherited method on a class instance to that instance
  • An auto-bind/react entry point that automatically excludes React’s class-component lifecycle methods from binding
  • include/exclude options accepting strings or RegExp patterns to selectively bind a subset of methods
  • Full TypeScript type definitions (index.d.ts) with a generic signature that preserves the input class’s shape
  • Correct handling of Symbol-keyed methods and getters that throw, verified by the test suite

Common Use Cases

  • Binding event handlers in class-based React components without writing this.handleClick = this.handleClick.bind(this) for each method
  • Passing class methods directly as callbacks (e.g. to array methods or event emitters) without losing this context
  • Libraries that hand back a class instance and want consumers to be able to destructure and call its methods safely
  • Codebases that avoid class-field syntax and need a single call to bind an entire instance at once

Under The Hood

Architecture The package is a single-file module with no internal layering: index.js exports one function (autoBind) built around a small getAllProperties helper that walks the prototype chain with Reflect.ownKeys/Reflect.getPrototypeOf, collecting [object, key] pairs into a Set until it reaches Object.prototype. autoBind then filters that set through an optional include/exclude matcher and rebinds each function-valued own property descriptor to the instance. react.js is a thin wrapper that pre-seeds exclude with React’s class lifecycle method names and delegates entirely to index.js — there is no abstraction boundary between the two, so any change to the core binding loop propagates directly to the React entry point and to every consumer’s bound methods.

Tech Stack Pure vanilla JavaScript (ES2020+ features: Reflect, Set, object spread) published as native ESM ("type": "module") with zero runtime dependencies. Dev tooling is xo (an opinionated ESLint preset) for linting, ava for the test runner, and tsd for type-testing index.d.ts against index.test-d.ts. There is no build/bundle step — the package ships its source files directly via the exports map (.index.js, ./reactreact.js), targeting Node ^12.20.0 || ^14.13.1 || >=16.0.0 per engines.

Code Quality The ava test suite covers plain binding, the include and exclude options, Symbol-keyed methods, inherited properties across two levels of subclassing, and the React wrapper’s lifecycle exclusion — a focused, adequate set of cases for the module’s small surface area. There’s no explicit error handling because the logic is purely synchronous property inspection; type safety is enforced separately via index.d.ts checked by tsd. A GitHub Actions workflow (.github/workflows/main.yml) runs npm test (xo + ava + tsd) on push and pull request.

What Makes It Unique The library’s main distinguishing choice is ergonomic rather than technical: a dedicated /react subpath export saves consumers from having to know and hand-maintain React’s list of lifecycle method names themselves. The README is unusually candid that the whole pattern is a stopgap — it directly recommends native class-field syntax (method = () => {}) as the modern alternative for Node 12+ and browser targets, rather than positioning auto-bind as a permanent solution.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search