derive

2.3.0 • Public • Published

derive NPM version

A utility to either derive from multiple super-constructors, or inherit from one super-constructor.

Note

You shouldn't rely on getters or setters with side effects (like the Array's length property) when deriving from native types. (You probably shouldn't derive or inherit from native types anyway)

Also, the instanceof operator will not work as expected, because it checks if a prototype in the chain is equal to the prototype of the object it's being checked against - which means it will return false for everything you've derived from.

That's why it's called derive, not inherit.

Install with npm

$ npm install derive

Install with bower

$ bower install derive

Usage

Deriving from multiple super-constructors:

// Your constructor function
function Example() {
  // Don't forget to call the
  // super's constructors
  EventEmitter.call( this )
  Array.call( this )
}
 
// Your prototype
Example.prototype = {
  constructor: Example,
  get bla() { return 1 },
  method: function() {
    // ...
  }
}
 
derive( Example, EventEmitter, Array )

Inheriting from a super-constructor:

function Example() {
  Emitter.call( this )
}
 
Example.prototype = {
  constructor: Example,
  set setter( value ) {},
  method: function noop() {
    // ...
  }
}
 
derive.inherit( Example, Emitter )

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 2.3.0
    243
    • latest

Version History

  • Version
    Downloads (Last 7 Days)
    • Published
  • 2.3.0
    243
  • 2.2.0
    1
  • 2.1.0
    1
  • 1.0.0
    0

Package Sidebar

Install

npm i derive

Weekly Downloads

16

Version

2.3.0

License

MIT

Last publish

Collaborators

  • jhermsmeier