Socket
Socket
Sign inDemoInstall

bind-decorator

Package Overview
Dependencies
0
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

bind-decorator

The fastest automatic method.bind(this) decorator


Version published
Weekly downloads
147K
increased by11.12%
Maintainers
1
Created
Weekly downloads
 

Package description

What is bind-decorator?

The bind-decorator npm package provides a simple way to bind class methods to the instance of the class. This is particularly useful in JavaScript and TypeScript when dealing with event handlers or callbacks where the context of 'this' can be lost.

What are bind-decorator's main functionalities?

Method Binding

This feature allows you to bind a class method to the instance of the class using the @bind decorator. This ensures that the method retains the correct 'this' context when called.

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

  @bind
  printValue() {
    console.log(this.value);
  }
}

const instance = new MyClass();
const print = instance.printValue;
print(); // Outputs: 42

Other packages similar to bind-decorator

Readme

Source

bind-decorator

Context method binding decorator.

npm version license Build Status Code Climate Test Coverage Issue Count TypeScript Typings

@bind is just a little faster version of @autobind for decorating methods only, by binding them to the current context. It is written in TypeScript and follows the latest decorators proposal.

  • It will throw exceptions if decorating anything other than function;
  • Since the implementation follows the latest decorators proposal where compartion betweeen this and target can not be trusted, @bind will always return a configurable, get accessor propertyDescriptor which will memomize the result of descriptor.value.bind(this) by re-defining the property descriptor of the method beeing decorated (Credits goes to autobind-decorator for memoizing the result).

If you are looking for not just method decorator but rather full class bounding decorator check @autobind.

Install

Install with npm:

$ npm install bind-decorator

NPM

Usage

In JavaScript

import bind from 'bind-decorator';

class Test {
    static what = 'static';
    
    @bind
    static test() {
        console.log(this.what);
    }

    constructor(what) {
        this.what = what;
    }

    @bind
    test() {
        console.warn(this.what);
    }
}

const tester = new Test('bind');
const { test } = tester;
tester.test(); // warns 'bind'.
test(); // warns 'bind'.
Test.test(); // logs 'static'.

In TypeScript

import bind from 'bind-decorator';

class Test {
    public static what: string = 'static';
    
    @bind
    public static test(): void {
        console.log(this.what);
    }

    public constructor(public what: string) {
        this.what = what;
    }

    @bind
    public test(): void {
        console.warn(this.what);
    }
}

const tester: Test = new Test('bind');
const { test } = tester;
tester.test(); // warns 'bind'.
test(); // warns 'bind'.
Test.test(); // logs 'static'.

Testing

  1. npm install

  2. npm test

Contributing

  1. npm install

  2. Make changes

  3. If necessary add some tests to __tests__

  4. npm test

  5. Make a Pull Request

Keywords

FAQs

Last updated on 20 Jul 2017

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

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc