auto-bind
Automatically bind class methods to their instance, including inherited ones
Repository Health
Technical Analysis
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/reactentry point that automatically excludes React’s class-component lifecycle methods from binding include/excludeoptions 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
thiscontext - 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, ./react → react.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.
Used by 4 apps in this directory
isoflow
Design Tools · Developer Tools
A React component that lets developers embed a fully-featured, isometric network diagram editor directly into their applications.
NocoDB
No Code Platforms · Databases · Low Code Platforms
Turn any SQL database into a collaborative no-code spreadsheet with automatic REST APIs and real-time views.
openclaude
AI Agents · AI Code Assistants
Run Claude Code workflows against any LLM — OpenAI, Gemini, Ollama, and 200+ backends — from a single terminal-first CLI.
TinaCMS
CMS
An open-source, Git-backed headless CMS that gives editors a live visual editing UI over Markdown, MDX, JSON, and YAML content while developers keep everything in version control.