Socket
Socket
Sign inDemoInstall

auto-bind

Package Overview
Dependencies
0
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    auto-bind

Automatically bind methods to their class instance


Version published
Weekly downloads
2.5M
decreased by-21.48%
Maintainers
1
Install size
7.67 kB
Created
Weekly downloads
 

Package description

What is auto-bind?

The auto-bind npm package is used to automatically bind methods of a class to their instance. This is particularly useful in React components to avoid having to manually bind 'this' in the constructor for every method that needs it.

What are auto-bind's main functionalities?

Automatic binding of class methods

This feature automatically binds all methods of a class instance to the instance itself, ensuring that 'this' within the methods refers to the class instance even when the method is extracted and called separately.

const autoBind = require('auto-bind');

class MyClass {
  constructor() {
    autoBind(this);
  }

  myMethod() {
    // method body that uses 'this'
  }
}

const instance = new MyClass();
const { myMethod } = instance;
myMethod(); // 'this' is correctly bound to the instance

Other packages similar to auto-bind

Readme

Source

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);
	}

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

Keywords

FAQs

Last updated on 18 Oct 2021

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc