MCPcopy Create free account
hub / github.com/sindresorhus/auto-bind

github.com/sindresorhus/auto-bind @v5.0.1

Chat with this repo
repository ↗ · DeepWiki ↗ · release v5.0.1 ↗ · + Follow
28 symbols 42 edges 6 files 0 documented · 0% 16 cross-repo links updated 4y agov5.0.1 · 2021-10-18★ 462

Browse by type

Functions 17 Types & classes 11
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

auto-bind

Automatically bind methods to their class instance

It also correctly binds inherited properties.

Install

npm install auto-bind

Usage

import autoBind from 'auto-bind';

class Unicorn {
    constructor(name) {
        this.name = name;
        autoBind(this);
    }

    message() {
        return `${this.name} is awesome!`;
    }
}

const unicorn = new Unicorn('Rainbow');

// Grab the method off the class instance
const message = unicorn.message;

// Still bound to the class instance
message();
//=> 'Rainbow is awesome!'

// Without `autoBind(this)`, the above would have resulted in
message();
//=> Error: Cannot read property 'name' of undefined

API

autoBind(self, options?)

Bind methods in self to their class instance.

Returns the self object.

self

Type: object

An object with methods to bind.

options

Type: object

include

Type: Array<string | RegExp>

Bind only the given methods.

exclude

Type: Array<string | RegExp>

Bind methods except for the given methods.

React

Same as autoBind but excludes the default React component methods.

import autoBindReact from 'auto-bind/react';

class Foo extends React.Component {
    constructor(props) {
        super(props);
        autoBindReact(this);
    }

    // …
}

Related

  • bind-methods - Bind all methods in an object to itself or a specified context

Extension points exported contracts — how you extend this code

browse all types & interfaces →

Core symbols most depended-on inside this repo

browse all functions →

Shape

Method 11
Class 10
Function 6
Interface 1

Languages

TypeScript100%

Modules by API surface

test.js15 symbols
index.test-d.ts7 symbols
index.js4 symbols
react.js1 symbols
index.d.ts1 symbols

For agents

$ claude mcp add auto-bind \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page