Socket
Socket
Sign inDemoInstall

inheritz

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

inheritz

Node.JS inherits replacement with support for mixins


Version published
Weekly downloads
3
Maintainers
1
Weekly downloads
 
Created
Source

inheritz

Node.JS 'inherits' replacement that supports mixins.

Call it like the normal Node.JS 'inherits' and optionally supply additional arguments to add as mixins. All prototype methods of mixin constructors are added to the prototype of the class.

If inheritz is called on a constructor that already inherits from something (ie, it already has prototype methods), then all parameters are added as mixins. This makes it useful for adding mixins to ES6 classes.

var inherits = require('inheritz');

function SuperClassA() {}
SuperClassA.prototype.methodA = function() {
	return 'A';
};

function SuperClassB() {}
SuperClassB.prototype.methodB = function() {
	return 'B';
};

function SuperClassC() {}
SuperClassC.prototype.methodC = function() {
	return 'C';
};

function MyClass() {
	SuperClassA.call(this);
	SuperClassB.call(this);
	SuperClassC.call(this);
}

inherits(MyClass, SuperClassA, SuperClassB, SuperClassC);

var myClass = new MyClass();

myClass.methodA();
myClass.methodB();
myClass.methodC();

By default, if a method on a mixin conflicts with an existing method already belonging to the prototype, the mixin will yield to the existing method.

var inherits = require('inheritz');

function SuperClassA() {}
SuperClassA.prototype.method = function() {
	return 'A';
};

function SuperClassB() {}
SuperClassB.prototype.method = function() {
	return 'B';
};

function MyClass() {
	SuperClassA.call(this);
	SuperClassB.call(this);
}

inherits(MyClass, SuperClassA);
inherits(MyClass, SuperClassB);

var myClass = new MyClass();

console.log(myClass.method()); // prints 'A'

However, if you supply a boolean true as the first parameter to inheritz, this behavior is reversed:

var inherits = require('inheritz');

function SuperClassA() {}
SuperClassA.prototype.method = function() {
	return 'A';
};

function SuperClassB() {}
SuperClassB.prototype.method = function() {
	return 'B';
};

function MyClass() {
	SuperClassA.call(this);
	SuperClassB.call(this);
}

inherits(MyClass, SuperClassA);
inherits(true, MyClass, SuperClassB);

var myClass = new MyClass();

console.log(myClass.method()); // prints 'B'

Keywords

FAQs

Package last updated on 16 Jul 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc