This is my solution to the problem. You can "implement" multiple interfaces by overriding one interface with another.
class MyInterface {
EDIT:
I improved the code, so now you can just use the implement (baseClass, interface1, interface2, ...) in the extension.
/ **
* Implements any number of interfaces for this class.
* @param cls The class you want to use
* @param interfaces Any number of comma separated interfaces
* @return The cls class is extended by all methods of all implemented interfaces.
* /
function implementation (cls, ... interfaces) {
let clsPrototype = Object.getPrototypeOf (cls) .prototype;
for (let me = 0; I <interfaces.length; i ++) {
let proto = interfaces [i] .prototype;
for (let methodName of Object.getOwnPropertyNames (proto)) {
if (methodName! == 'constructor')
if (typeof proto [methodName] === 'function')
if (! clsPrototype [methodName]) {
console.warn ('WARNING! "+ method name +' of" Interface "'+ interfaces [i]. name +'" is not declared in the class "'+ cls. name +'" ');
clsPrototype [methodName] = proto [methodName];
}
}
}
refund cls;
}
// Basic interface for warning when an undefined method is used
class MyBaseInterface {
// declare a warning generator to see if the interface method is overridden
// Requires a function name of the Interface method or any String that gives you a hint;)
_WARNING (fName = 'unknown method') {
console.warn ('WARNING! Function' '+ f name +' "is not overridden in + this.constructor.name);
}
}
// create your own class
/ * This is the simplest example, but you can also use
*
* class MyCustomClass1 extends implementation (MyBaseInterface) {
* foo () {return 66;}
*}
*
* /
class MyCustomClass1 extends MyBaseInterface {
foo () {return 66;}
}
// create user interface
class MyCustomInterface1 {
// Declare your JS document in the interface to make it accessible when writing a class and for subsequent inheritance
/ **
* Gives the sum of the given numbers
* @param {Number} a First number
* @param {Number} b Second number
* @return {Number} Sum of numbers
* /
sum (a, b) {this._WARNING ('sum (a, b)'); }
}
// and other user interface
class MyCustomInterface2 {
/ **
* Gives the square of a given number
* @param {Number} a The Number
* @return {Number} Square Numbers
* /
() {this._WARNING (' ()'); }
}
// Extend your user class even further and implement user interfaces
the AllInterfacesImplemented class extends the implementation (MyCustomClass1, MyCustomInterface1, MyCustomInterface2) {
/ **
* @inheritdoc
* /
sum (a, b) {return a + b; }
/ **
* Multiplies two numbers
* @param {Number} a First number
* @param {Number} b Second number
* @return {Number}
* /
multiply (a, b) {return a * b;}
}
// TRY THIS
let x = new AllInterfacesImplemented ();
console.log ("x.foo () =", x.foo ());
// output: 'x.foo () = 66'
console.log ("x.square (2) =", x.square (2));
// output: 'x.square (2) = undefined
// the console warns: "ATTENTION! The function" square (a) "is not redefined in AllInterfacesImplemented '
console.log ("x.sum (1, 2) =", x.sum (1, 2));
// output: 'x.sum (1, 2) = 3'
console.log ("x.multiply (4, 5) =", x.multiply (4, 5));
// output: 'x.multiply (4, 5) = 20'
Kai Lehmann Apr 30 '19 at 7:41 2019-04-30 07:41
source share