1.0.11 • Published 8 years ago

bind-decorator v1.0.11

Weekly downloads
30,626
License
MIT
Repository
github
Last release
8 years ago

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

taro-ui-xuilottiereact-git-providerreact-git-taskbarnpm-ctsreact-material-ui-keyboard-caitaro-pizza-uihlj-weapp-uipizza_uiskyhook-ui@everything-registry/sub-chunk-1236taro-ui-arlentaro-ui-fixtaro-ui-hello-redtaro-ui-hqwlkjtaro-ui-jtest01taro-ymuitaro-tools-uitaro-ui-testtaro-ui-waytaro-ui-xtaro-ui2taro-ui2-fixtaro-materialtaro-mstwizard-decoratorswalletlinkwalletlink-evue-i18next2xbz-taroxbzui-tarotest-publish-taro-toolsverdaccio-oidc@cmmn/coretaro-ui-shistrike-uitt-uitypedsats-wc-react@amc-technology/applicationangularframework@admin-layout/user-auth0-mobile@adminide-stack/platform-browser@01js/taro-ui@aardvarkxr/aardvark-react@aardvarkxr/aardvark-shared@danizean/cyberzoneyqh-base-uiyqh-cm-uizelda-mp-uizigbee2mqttzona-xzo-taro-uizo-ui-jr@parallelnft/web3modal@fimbul/wotan@hanyun/taro-ui@ifanshx/cycgods@eine-nineteen/eine@embroider/compat@leapcloud/taro-ui@lylb/tzo-ui@mathiasarens/fints@preact-material-components/linear-progress@preact-material-components/menu-surface@preact-material-components/radio@preact-material-components/textfield@preact-material-components/button@preact-material-components/card@preact-material-components/checkbox@preact-material-components/chip@preact-material-components/elevation@preact-material-components/fab@preact-material-components/icon@publica.re/react-git-provider@publica.re/react-git-taskbar@pwrs/mixins@real_ate/fake-embroider-compat@philippdormann/fints@pooltogether/walletlink@pacoyang/taro-ui@soyjak/utilsms-wx-uimy-taro-uimobx-repositorynpm-package-json-loaderpekon-articlepekon-blankpekon-characterpekon-messagepekon-picture-adspekon-ugcpekon-videoreact-component-refrtaro-uireact-material-ui-keyboardreact-material-ui-keyboard-no-touch-tapreact-material-touch-keyboardremestpreact-material-componentsserverless-yandex-cloud
1.0.11

8 years ago

1.0.10

9 years ago

1.0.9

9 years ago

1.0.8

9 years ago

1.0.7

9 years ago

1.0.6

9 years ago

1.0.5

9 years ago

1.0.4

9 years ago

1.0.3

9 years ago

1.0.2

9 years ago

1.0.1

9 years ago

1.0.0

9 years ago

0.0.1-pre

9 years ago