5.0.1 • Published 3 years ago

auto-bind v5.0.1

Weekly downloads
1,039,085
License
MIT
Repository
github
Last release
3 years ago

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

	// …
}

Related

  • bind-methods - Bind all methods in an object to itself or a specified context
@graphql-codegen/typescript@graphql-codegen/java-common@graphql-codegen/flow@graphql-codegen/java-apollo-android@graphql-codegen/flow-resolvers@graphql-codegen/flow-operations@graphql-codegen/typescript-document-nodes@graphql-codegen/typescript-urql@graphql-codegen/visitor-plugin-common@graphql-codegen/typescript-stencil-apollo@graphql-codegen/typescript-type-graphql@graphql-codegen/typescript-graphql-request@graphql-codegen/typescript-resolvers@graphql-codegen/typescript-operations@graphql-codegen/typescript-react-apollo@graphql-codegen/typescript-apollo-angular@graphql-codegen/typescript-mongodb@tinacms/clisonos-radio-salietchelon-gql-typescript-apollo-angular@stelladoradus/hermes-cli@stelladoradus/stella-cli@ourharvest/email-templates@xornot/boundnocodb1rc-crudweb-hook-test-lambda@prodam/prodam-types@lightsparkdev/js-sdknocodb-daily@everything-registry/sub-chunk-1184@imtbl/imxd-clientimx-sdk@slide-web3/sdk@imean/typescript-graphql-request@graphql-codegen/typescript-vue-apollo@graphql-codegen/typescript-vue-apollo-smart-ops@graphql-codegen/typescript-vue-urql@graphql-codegen/urql-svelte-operations-store@graphql-codegen/gql-tag-operations@graphql-codegen/c-sharp-apollo@graphql-codegen/flutter-freezed@graphql-codegen/c-sharp-common@graphql-codegen/c-sharp-operations@graphql-codegen/typescript-generic-sdk@graphql-codegen/typescript-graphql-apollo@graphql-codegen/typescript-jit-sdk@graphql-codegen/typescript-msw@graphql-codegen/typescript-nest@graphql-codegen/typescript-oclif@graphql-codegen/typescript-react-query@graphql-codegen/typescript-rtk-query@graphql-codegen/typescript-solid-query@graphql-codegen/typescript-urql-graphcache@graphql-codegen/typescript-react-offix@hooleyandburch/gridsome@gpnwebassistant/gpn-web-gateway-js@infinitebrahmanuniverse/nolb-auto-@jbarrus/typescript-apollo-angular@iohk/mallet@leondreamed/ink@jmvtrinidad/chimp-graphql-codegen-plugin@junglesys/gridsome@jshaftoe/infra@mfellner/graphql-codegen-typescript-urql-sdk@nextswitch/nssdk@neil585456525/typescript-msw@neil585456525/typescript-react-query@neon.id/fastify@nixela/typescript-graphql-request@n44ps/typescript-react-query@ninetailed/typescript-cf-worker-graphql-request@ladjs/logger@ladjs/bull@k8slens/open-lens@frontside/ink@gaoding/gd-tracker@gallery-so/typescript-operations@gallery-so/visitor-plugin-common@gladysassistant/gladys-gateway-js@getstation/graphql-codegen-typescript-reactive@getstation/graphql-codegen-typescript-reactive-resolvers@gnd/ink@google/chat-sdk@madsci/graphql-codegen-nestjs@mahonbr/component-factory@lvi/ink@m92/crypto@m92/es-odm@m92/express-utils@m92/jwt@lightsparkdev/react-native@lightsparkdev/react-wallet@lightsparkdev/uma@marcellejs/core@greatnonprofits-nfp/temba-analytics@graphql-codegen/typed-document-node@graphql-codegen/typescript-apollo-client-helpers@graphitation/graphql-codegen-typescript-operations@grund/auth-utils
5.0.1

3 years ago

5.0.0

3 years ago

4.0.0

4 years ago

3.0.0

5 years ago

2.1.1

5 years ago

2.1.0

5 years ago

2.0.0

5 years ago

1.2.1

6 years ago

1.2.0

6 years ago

1.1.0

7 years ago

1.0.0

7 years ago

0.1.0

8 years ago