Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

class-autobind

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

class-autobind

Method auto-bind for ES6 (ES2015) classes

  • 0.1.4
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
62K
decreased by-3.93%
Maintainers
1
Weekly downloads
 
Created
Source

Method auto-bind for ES6 (ES2015) classes

This package provides a single function, autobind, for use within a constructor to bind all methods to the instance itself.

For example, this allows us to pass a method to an event handler element.addEventListener('click', this.onClick) and be sure the onClick method will always be called with the right context.

Note: This has some specific logic for React, but could be used in any project.

Usage:

import autobind from 'class-autobind';

class MyComponent extends React.Component {
  constructor() {
    super(...arguments);
    autobind(this);
  }
  render() {
    return <button onClick={this.onClick}>Click Me</button>;
  }
  onClick() {
    console.log('Button Clicked');
  }
}

Advanced Usage:

If your component will possibly be subclassed (you really should not do this, but some third-party libraries like react-css-modules do so) then you will need to specify which prototype will be the source of methods that are to be automatically bound.

import autobind from 'class-autobind';

class MyComponent extends React.Component {
  constructor() {
    super(...arguments);
    autobind(this, MyComponent.prototype); // Note the second parameter.
  }
  render() {
    /* ... */
  }
}

class MySubClassedComponent extends MyComponent {
  /* This is probably a very bad idea. */
}

License

This software is BSD Licensed.

Keywords

FAQs

Package last updated on 13 Jun 2016

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc