Join our webinar on Wednesday, June 26, at 1pm EDTHow Chia Mitigates Risk in the Crypto Industry.Register
Socket
Socket
Sign inDemoInstall

autobind-decorator

Package Overview
Dependencies
0
Maintainers
1
Versions
19
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    autobind-decorator

Decorator for binding method to an object


Version published
Weekly downloads
253K
decreased by-5.28%
Maintainers
1
Install size
6.25 kB
Created
Weekly downloads
 

Package description

What is autobind-decorator?

The `autobind-decorator` npm package provides a decorator for automatically binding methods to their class instance. This is particularly useful in JavaScript and TypeScript classes where you want to ensure that methods retain the correct `this` context, especially when passing them as callbacks.

What are autobind-decorator's main functionalities?

Automatic Method Binding

This feature allows you to automatically bind class methods to the instance of the class. In the example, the `getValue` method is bound to the instance of `MyClass`, ensuring that `this.value` correctly refers to the instance's `value` property even when the method is called as a standalone function.

class MyClass {
  constructor() {
    this.value = 42;
  }

  @autobind
  getValue() {
    return this.value;
  }
}

const instance = new MyClass();
const getValue = instance.getValue;
console.log(getValue()); // 42

Other packages similar to autobind-decorator

Readme

Source

autobind decorator

This is a method decorator which is when applied to a method binds it to an object so this always points to an object instance within a method.

As decorators are a part of future ES7 standard they can only be used with transpilers such as Babel.

Installation:

% npm install autobind-decorator

Example:

import autobind from 'autobind-decorator'

class Component {

  constructor(value) {
    this.value = value
  }

  @autobind
  method() {
    return this.value
  }
}

let component = new Component(42)
let method = component.method // .bind(component) isn't needed!
method() // returns 42

FAQs

Last updated on 11 Apr 2015

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