# attatch-to-prototype

> Attach functions as non enumerable properties to the prototype of any object.

Latest version **1.3.21** (published 2023-07-22) · ISC license · 0 weekly downloads

## Install

```sh
npm install attatch-to-prototype
pnpm add attatch-to-prototype
yarn add attatch-to-prototype
bun add attatch-to-prototype
```

## Health

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

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.3.21 |
| Published | 2023-07-22 |
| First published | 2020-02-03 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 25.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Maximilian Mairinger |
| Maintainers | zzrv |
| Keywords | attach, prototype, assign, property |

## Links

- npm: https://www.npmjs.com/package/attatch-to-prototype
- Repository: https://github.com/maximilianMairinger/attatchToPrototype
- Homepage: https://github.com/maximilianMairinger/attatchToPrototype#readme
- Issues: https://github.com/maximilianMairinger/attatchToPrototype/issues
- npm.io page: https://npm.io/package/attatch-to-prototype

## Dependencies (1)

- [circ-clone](https://npm.io/package/circ-clone.md) ^2.1.4

## Recent versions

- 1.3.21 (latest) — 2023-07-22
- 1.3.20 — 2023-06-05
- 1.3.19 — 2023-05-07
- 1.3.18 — 2023-05-07
- 1.3.17 — 2022-04-07
- 1.3.16 — 2022-02-24
- 1.3.15 — 2022-02-24
- 1.3.14 — 2022-02-22
- 1.3.13 — 2022-02-22
- 1.3.12 — 2021-12-01
- 1.3.11 — 2021-01-16
- 1.3.10 — 2021-01-05
- 1.3.9 — 2021-01-04
- 1.3.8 — 2020-12-05
- 1.3.7 — 2020-12-04
- … 18 more at https://npm.io/package/attatch-to-prototype/versions

## README

# Attach to prototype

Attach functions as non enumerable properties to the prototype of any object.

> Please not that Attach to prototype is currently under development and not yet suited for production

## Example

### Generic

```ts
import { constructAttachToPrototype } from "attatch-to-prototype"

const attachToArray = constructAttachToPrototype(Array.prototype/*, options*/)
attachToArray("removeByValue", function(value) {
  const index = this.indexOf(value)
  if (index === -1) return
  this.splice(index, 1)
})

const ar = ["a", "b", "c"]

ar.removeByValue("b")

console.log(ar) // ["a", "c"]

```
### Getter / Setter

#### Attach

```ts
attachToArray("last", {
  get() {
    return this[this.length-1]
  }
  set(to) {
    return this[this.length-1] = to
  }
})


let ar = [1, 2, 3]

ar.last      // return 3
ar.last = 0  // return 0

console.log(ar) // [1, 2, 0]
```


#### Apply

Just a different syntax to access getter / setter. Use Attach / apply depending on your coding conventions.

```ts
import { constructApplyToPrototype } from "attatch-to-prototype"

const attachToArray = constructApplyToPrototype(Array.prototype/*, options*/)
attachToArray("last", {
  get() {
    return this[this.length-1]
  }
  set(to) {
    return this[this.length-1] = to
  }
})


let ar = [1, 2, 3]

ar.last()   // return 3
ar.last(0)  // return 0

console.log(ar) // [1, 2, 0]
```

The [generic](#Generic) functionality is also available on apply

## Contribute

All feedback is appreciated. Create a pull request or write an issue.

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