Skip to content

danielFHcode/class-merger

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 

Repository files navigation

Warning

This version of class-merger has been deprecated. The source code has been moved to https://github.com/danielFHcode/class-merger-2

Class Merger

License NPM

Table of contents

About

Class Merger is a simple one-function library for creating multi inheritance classes, inspired by the "real" class expression mixin method.

Installation

To install you can either use npm:

npm i class-merger

Or just download the one file from github.

To include the file in client side js use a script tag:

<body>
  <script src="/path/to/classMerger.js" defer></script>
</body>

To include the file in node js use require:

// There is no need to import anything but the mergeClasses function, since there is nothing.
const { mergeClasses } = require("class-merger");

Usage

Basic Usage

As mentioned earlier, Class Merger is a one-function library - meaning it only consist of one function.

And that one function is the mergeClasses function, which you can use like so:

class a {
  x() {}
}
class b {
  y() {}
}
class c extends mergeClasses(a, b) {
  z() {}
}
/*
Expected result: A class named c with methods x, y & z.
*/

Using Super

The specialty of using the class expression based mixin method is that you can use built in js inheritance options such as super:

class a {
  x() {}
}
class b {
  y() {}
}
class c extends mergeClasses(a, b) {
  z() {
    super.x();
    super.y();
  }
}
/*
Expected result: A class named c with a working super object containing methods from classes a & b.
*/

Extend Class After Creation

It's also worth noting you can use this library for extending classes after you create them. You see, because Class Merger uses the class expression based mixin method, when you write:

class a{}
class b{}
class c{}
mergeClasses(a,b,c)

What essentially happens is:

class c{}
class b extends c{}
class a extends b{}
// Note that classes b & c aren't actually modified, but rather newly created copies of them are.

So, you could use that to your advantage, and do something like this to extend classes after you created them:

class a{}
class b{}
class c{}

a = mergeClasses(
  a, /* extends */ b, c
)

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published