unherit

Clone a constructor without affecting the super-class

Usage no npm install needed!

<script type="module">
  import unherit from 'https://cdn.skypack.dev/unherit';
</script>

README

unherit

Build Coverage Downloads Size

Create a custom constructor which can be modified without affecting the original class.

Install

This package is ESM only: Node 12+ is needed to use it and it must be imported instead of required.

npm:

npm install unherit

Use

import {EventEmitter} from 'events'
import {unherit} from 'unherit'

// Create a private class which acts just like `EventEmitter`.
var Emitter = unherit(EventEmitter)

Emitter.prototype.defaultMaxListeners = 0
// Now, all instances of `Emitter` have no maximum listeners, without affecting
// other `EventEmitter`s.

new Emitter().defaultMaxListeners === 0 // => true
new EventEmitter().defaultMaxListeners === undefined // => true
new Emitter() instanceof EventEmitter // => true

API

This package exports the following identifiers: unherit. There is no default export.

unherit(Super)

Create a custom constructor (Function) from Super (Function) which can be modified without affecting the original class.

License

MIT © Titus Wormer