You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+46Lines changed: 46 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,6 +2,48 @@
2
2
3
3
This library can be used to achieve multiple inheritance in JavaScript, but it sets itself apart from other libraries for multiple inheritance because it isolates the base classes and prevents the properties and methods from colliding, while still allowing easy access to the properties and methods of the base classes.
To inherit from multiple bases simply extend from `bases(Class1, Class2, ...)`. In the constructor pass the <b>*instances*</b> of the classes to the `super` constructor.
34
+
35
+
Then you can access the base properties and methods directly from `this.`, the first class with that property or method will be given precedence.
36
+
37
+
To access a particular base use `this.bases[index]`.
38
+
39
+
Replace all instances of `v instanceof Class` with `isInstanceOf(v, Class)`, it’s just that simple.
40
+
41
+
## For more details
42
+
43
+
- Read the [Handy manual](https://github.com/aryan-programmer/extend-bases/wiki/Handy-Manual), it’s very simple & small and gives enough information to get started.
44
+
- Then read the [Documentation](https://github.com/aryan-programmer/extend-bases/wiki/Documentation). it’s not too big and easy to go through.
45
+
- Then, if you have enough time or are interested, read the tests and source code of the project.
46
+
5
47
## Implementation details
6
48
7
49
This library uses Proxies, so sadly no IE11 support, but otherwise this library also imports es-shims for `Reflect.ownKeys`(ES6 but used very often and an es-shim exists for it) and `Array.prototype.flatMap`(ES2019, very new), which were found to be the bottlenecks, for some browsers.
@@ -11,3 +53,7 @@ This library uses proxies for easy access in the class instances, and it merges
11
53
## TypeScript support
12
54
13
55
The library is by default written in TypeScript, which you all should be using if you aren’t, for great code completion. The details of the TypeScript definitions are a bit fiddly, so it’s best not to worry about them, they work<small>, or you can just look at the source code</small>.
56
+
57
+
## Contributing
58
+
59
+
Feel free to open a pull request, fix bugs, or read the source code. It’s all up to you.
@@ -47,7 +47,7 @@ type HasBasesArray<TBases extends Ctor[]> = {
47
47
}
48
48
49
49
/**
50
-
* Specifies that the object is an intersection of all of the bases.
50
+
* Specifies that the object is an intersection of all of the bases, and has an array containing all of the base instances.
51
51
*/
52
52
typeHasBases<TBasesextendsCtor[]>=
53
53
UnionToIntersection<AnyInstancesOf<TBases>>&
@@ -108,6 +108,7 @@ function setPrototypeToProxy<TBases extends Ctor[]> (
108
108
* But, this library isolates the derived and base classes, ie prevents collision of their properties and methods.
109
109
* Thus, this problem can be avoided by using the <code>defineProperties</code> method from this library, if you use the <code>bases</code> methods as well.
110
110
* @param baseClasses The base classes to be inherited.
111
+
* @return A constructor taking in the *instances* of the base classes.
111
112
*
112
113
* @example
113
114
* class Activatable {
@@ -263,7 +264,7 @@ function bases<TBases extends Ctor[]> (...baseClasses: TBases):
263
264
264
265
/**
265
266
* Defines the properties on the given object, the key represents the name of the property and the value as the, well, value.
266
-
* Moreover, if the property name if prefixed with "readonly " then the property will be set to be readonly, ie non-writable.
267
+
* Moreover, if the property name if prefixed with `readonly` then the property will be set to be readonly, ie non-writable, ie any attempts to edit it in strict mode will fail with a `TypeError`.
267
268
*
268
269
* Use this function to set the properties of the objects inheriting from multiple base classes.
269
270
*
@@ -277,7 +278,7 @@ function bases<TBases extends Ctor[]> (...baseClasses: TBases):
277
278
* "readonly <>": <value>, // Define a readonly property on `this` with the name <prop> and value <value>, readonly ie any attempts to edit it in strict mode will fail with a TypeError.
0 commit comments