# extend-prototype

> Extends one class with methods from another.

Latest version **1.1.0** (published 2017-11-27) · MIT license · 0 weekly downloads

## Install

```sh
npm install extend-prototype
pnpm add extend-prototype
yarn add extend-prototype
bun add extend-prototype
```

## 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 | 2017-11-27 |
| First published | 2017-01-29 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=7.0.0 |
| Dependencies | 3 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | John Lamansky |
| Maintainers | lamansky |
| Keywords | function, class, prototype, mixin, trait, extend, method, getter, setter |

## Links

- npm: https://www.npmjs.com/package/extend-prototype
- Repository: https://github.com/lamansky/extend-prototype
- Issues: https://github.com/lamansky/extend-prototype/issues
- npm.io page: https://npm.io/package/extend-prototype

## Dependencies (3)

- [has-property](https://npm.io/package/has-property.md) ^1.0.0
- [get-prototype](https://npm.io/package/get-prototype.md) ^1.0.0
- [is-plain-object](https://npm.io/package/is-plain-object.md) ^2.0.1

## Recent versions

- 1.1.0 (latest) — 2017-11-27
- 1.0.0 — 2017-01-29

## README

# extend-prototype

* Extends one class with methods from another.
* Supports getters and setters.
* Can (optionally) make use of a whitelist.
* Can easily be added to a class as a static method.

Packaged as a [Node.js](https://nodejs.org/) module.

## Installation

```bash
npm install extend-prototype --save
```

The module exposes a single function.

## Function Parameters

* `target`: The object/class to extend.
* `sources`: An object/class (or an array of objects/classes) with methods to add to the target.
* `methodWhitelist`: An array of the names of methods to add. (If omitted, all methods are added.)
* `overwrite`: Whether or not to overwrite existing methods in the target.

## Function Signatures

The function can accept its arguments in a variety of combinations:

```javascript
extend(target, sources, methodWhitelist, {overwrite})
extend(target, sources, methodWhitelist)
extend(target, sources, {overwrite})
extend(target, sources)
source.extend(target, methodWhitelist, {overwrite})
source.extend(target, methodWhitelist)
source.extend(target, {overwrite})
source.extend(target)
```

## Usage Examples

### Basic Example

```javascript
const extend = require('extend-prototype')
class A {}
class B {
  test () {}
  get value () { return 'example' }
}
extend(A, B)
const a = new A()
a.test()
a.value // 'example'
```

### Static Method Example

Easily use `extend-prototype` as a static method on your mixin, like so:

```javascript
class Mixin {
  mixinMethod () {}
}
Mixin.extend = require('extend-prototype')

class Test {}
Mixin.extend(Test)

const test = new Test()
test.mixinMethod()
```

### Example with Arguments

The following example makes use of the `methodWhitelist` and `overwrite` parameters:

```javascript
const extend = require('extend-prototype')
class A {
  include () { return 'from A' }
}
class B {
  include () { return 'from B' }
  exclude () {}
}

// Only add the `include` method, and overwrite the existing one.
extend(A, B, ['include'], {overwrite: true})

const a = new A()
a.include() // 'from B'
typeof a.exclude // 'undefined'
```

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