unherit
Version:
Create a subclass that can be modified without affecting the super class
139 lines (88 loc) • 3.04 kB
Markdown
class.
* [What is this?](
* [When should I use this?](
* [Install](
* [Use](
* [API](
* [`unherit(Super)`](
* [Types](
* [Compatibility](
* [Contribute](
* [Security](
* [License](
This is a tiny package lets you create a subclass, that can be modified,
without affecting the super class.
Not often!
You might have some weird cases though.
This package is [ESM only][esm].
In Node.js (version 14.14+, 16.0+), install with [npm][]:
```sh
npm install unherit
```
In Deno with [`esm.sh`][esmsh]:
```js
import {unherit} from 'https://esm.sh/unherit@3'
```
In browsers with [`esm.sh`][esmsh]:
```html
<script type="module">
import {unherit} from 'https://esm.sh/unherit@3?bundle'
</script>
```
```js
import {EventEmitter} from 'node:events'
import {unherit} from 'unherit'
// Create a private class which acts just like `EventEmitter`.
const Emitter = unherit(EventEmitter)
Emitter.prototype.defaultMaxListeners = 0
// Now, all instances of `Emitter` have `0` maximum listeners, without affecting
// other `EventEmitter`s.
new Emitter().defaultMaxListeners === 0 // => true
new EventEmitter().defaultMaxListeners === undefined // => true
new Emitter() instanceof EventEmitter // => true
```
This package exports the identifier `unherit`.
There is no default export.
Subclass `Super`.
This package is fully typed with [TypeScript][].
It exports no additional types.
This package is at least compatible with all maintained versions of Node.js.
As of now, that is Node.js 14.14+ and 16.0+.
It also works in Deno and modern browsers.
Yes please!
See [How to Contribute to Open Source][contribute].
This package is safe.
[ ][license] © [Titus Wormer][author]
<!-- Definitions -->
[ ]: https://github.com/wooorm/unherit/workflows/main/badge.svg
[ ]: https://github.com/wooorm/unherit/actions
[ ]: https://img.shields.io/codecov/c/github/wooorm/unherit.svg
[ ]: https://codecov.io/github/wooorm/unherit
[ ]: https://img.shields.io/npm/dm/unherit.svg
[ ]: https://www.npmjs.com/package/unherit
[ ]: https://img.shields.io/bundlephobia/minzip/unherit.svg
[ ]: https://bundlephobia.com/result?p=unherit
[ ]: https://docs.npmjs.com/cli/install
[ ]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
[ ]: https://esm.sh
[ ]: https://www.typescriptlang.org
[ ]: https://opensource.guide/how-to-contribute/
[ ]: license
[ ]: https://wooorm.com
[![Build][build-badge]][build]
[![Coverage][coverage-badge]][coverage]
[![Downloads][downloads-badge]][downloads]
[![Size][size-badge]][size]
Create a subclass that can be modified without affecting the super