Skip to content

Commit

Permalink
Updated README and added static prop support.
Browse files Browse the repository at this point in the history
  • Loading branch information
danielFHcode committed Sep 27, 2022
1 parent 42d61fe commit fe4d124
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 5 deletions.
38 changes: 37 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ To include the file in client side js use a script tag:

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

Expand All @@ -40,6 +40,8 @@ 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:
Expand All @@ -59,6 +61,8 @@ Expected result: A class named c with methods x, y & z.
*/
```

### Using Super

The specialty of using the [class expression based mixin method](https://justinfagnani.com/2015/12/21/real-mixins-with-javascript-classes/) is that you can use built in js inheritance options such as `super`:

```js
Expand All @@ -78,3 +82,35 @@ class c extends mergeClasses(a, b) {
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](https://justinfagnani.com/2015/12/21/real-mixins-with-javascript-classes/), when you write:

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

What essentially happens is:

```js
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:

```js
class a{}
class b{}
class c{}

a = mergeClasses(
a, /* extends */ b, c
)
```
13 changes: 13 additions & 0 deletions classMerger.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,28 @@
* @returns {class} One merged class.
*/
function mergeClasses(...classList){
if (classList.length == 0) throw new Error("Can't merge class list of length 0");
if (classList.length == 1) return classList[0];

const currentClass = classList[0];
classList.splice(0,1);
const mergedClass = class extends mergeClasses(...classList){};

// For referencing default class properties.
const emptyClass = class{};

// Copying instance variables.
Object.getOwnPropertyNames(currentClass.prototype).forEach((name)=>{
// If property is equal to default there is no need to re-set it and potentially override the parent class's property.
if (emptyClass.prototype[name] == currentClass.prototype[name]) return;
mergedClass.prototype[name] = currentClass.prototype[name];
})

// Copying static properties.
Object.getOwnPropertyNames(currentClass).forEach((name)=>{
if (emptyClass[name] == currentClass[name]) return;
mergedClass[name] = currentClass[name];
})

return mergedClass;
}
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "class-merger",
"version": "1.0.0",
"description": "A simple one function library for multy inheritance OOP.",
"version": "1.0.1",
"description": "A simple one function library for multi inheritance OOP.",
"main": "classMerger.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
Expand All @@ -11,11 +11,11 @@
"url": "git+https://github.com/danielFHcode/class-merger.git"
},
"keywords": [
"[oop",
"oop",
"javaScript",
"multi",
"inheritance",
"inheritance]"
"inheritance"
],
"author": "Daniel F.H.",
"license": "ISC",
Expand Down

0 comments on commit fe4d124

Please sign in to comment.