# idempotent-bind

> idempotent `bind` function library.

Latest version **1.1.0** (published 2016-05-24) · MIT license · 0 weekly downloads

## Install

```sh
npm install idempotent-bind
pnpm add idempotent-bind
yarn add idempotent-bind
bun add idempotent-bind
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.0 |
| Published | 2016-05-24 |
| First published | 2015-05-10 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 15 |
| Author | azu |
| Maintainers | azu |
| Keywords | bind, events, listener, eventemitter |

## Links

- npm: https://www.npmjs.com/package/idempotent-bind
- Repository: https://github.com/azu/idempotent-bind
- Issues: https://github.com/azu/idempotent-bind/issues
- npm.io page: https://npm.io/package/idempotent-bind

## Dependencies (1)

- [es6-weak-map](https://npm.io/package/es6-weak-map.md) ^1.0.2

## Alternatives

- [async-exit-hook](https://npm.io/package/async-exit-hook.md) — 3.7M weekly downloads
- [evnty](https://npm.io/package/evnty.md) — 7.2K weekly downloads
- [eleventy-plugin-asciidoc](https://npm.io/package/eleventy-plugin-asciidoc.md) — 3.5K weekly downloads
- [@jswork/next-get2get](https://npm.io/package/@jswork/next-get2get.md) — 945 weekly downloads
- [@dashersw/axon](https://npm.io/package/@dashersw/axon.md) — 934 weekly downloads

## Recent versions

- 1.1.0 (latest) — 2016-05-24
- 1.0.1 — 2015-05-10

## README

# idempotent-bind [![Build Status](https://travis-ci.org/azu/idempotent-bind.svg?branch=master)](https://travis-ci.org/azu/idempotent-bind)

idempotent `bind` function library.

- [Idempotence - Wikipedia, the free encyclopedia](http://en.wikipedia.org/wiki/Idempotence "Idempotence - Wikipedia, the free encyclopedia")

## Abstract

A  `bind` function is idempotent if, whenever it is applied twice to any value, it gives the same result as if it were applied once:

```js
bind(bind(x, this), this) === bind(x, this)
```

ECMAScript' [`Function.prototype.bind (thisArg [, arg1 [, arg2, …]])`](http://ecma-international.org/ecma-262/5.1/#sec-15.3.4.5) is not idempotent.

```js
x.bind(this).bind(this) !== x.bind(this)
```

## Installation

```sh
npm install idempotent-bind
```

## Usage

### bind(target, thisArg)

The bind method takes two arguments, `target`, `thisArg`, and returns a bound function.

- `target`: the target function
- `thisArg`: The value to be passed as the this parameter to the target function when the bound function is called.

```js
import { bind, unbind } from "idempotent-bind"
import {EventEmitter} from "events"
const emitter = new EventEmitter();

class Component {
	componentWillMount(){
		emitter.on("change", bind(this.onChange, this));
	}
	onChange(){
		// do something
	}
	componentWillUnmount(){
		// `unbind` release `bind` from Reference Cache Map and return the bound function.
		emitter.removeListener("change", unbind(this.onChange, this));
		// == emitter.removeListener("change", bind(this.onChange, this));
		// but not release the Reference Cache Map.
	}
}
```


`bind` is not support `[, arg1 [, arg2, …]` like [`Function.prototype.bind (thisArg [, arg1 [, arg2, …]])`](http://ecma-international.org/ecma-262/5.1/#sec-15.3.4.5).


### unbind(target, thisArg)

The `unbind` method takes two arguments, `target`, `thisArg`, and **returns a bound function**.
This `unbind` behavior is similar to `removeChild`.

```js
var oldChild = element.removeChild(child) 
```

two arguments is the same as `bind` function.

- `target`: the target function
- `thisArg`: The value to be passed as the this parameter to the target function when the bound function is called.

```js
import { bind, unbind } from "idempotent-bind"
import assert from "assert"
var f = function () {};
var g = bind(f, this);
assert(g === bind(f, this));
// unbind!!
unbind(f, this);
assert(g !== bind(f, this));
```


## Tests

```sh
npm test
```

## Contributing

1. Fork it!
2. Create your feature branch: `git checkout -b my-new-feature`
3. Commit your changes: `git commit -am 'Add some feature'`
4. Push to the branch: `git push origin my-new-feature`
5. Submit a pull request :D

## License

MIT

---
_Source: https://npm.io/package/idempotent-bind · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
