Skip to content

Introduction

ALexeyP0708 edited this page Jul 2, 2020 · 2 revisions

If you are implementing an OOP-style API for JS and you need teamwork, then you will need to implement the Interfaces in your project. This component simulates the capabilities of Interfaces in JS when writing OOP code.

Exemple

export {InterfaceManager} from '.your_project/lib/Interfaces/src/export.js';
class MyTypeClass{};

//declare interface
class MyInterface{
/* implement method with default parameters 
(any arguments, returns any result).*/
	method(){} 
	
/* implement method2 */
	method2(){ 
	/*  criteria for method2 */
		return { 
			arguments:[
				{
					/* argument 1 must be type "string" or "undefined" or "null" */
					types:['string',undefined,null]
				}
			],
			return:{
			/* return result must be [object MyTypeClass] */
				types:[MyTypeClass] 
			}
		}
	} 
/* implement static_method */
	static static_method(){}

/* implement react property "react" */
	get react function(){
	/* criteria for getter */
		return {
			types:['string']
		}
	}
	set react function(v){
	/* criteria for setter */ 
		return {
			types:['string']
		};
	}
}
/* implement property "prop" */
MyInterface.prototype.prop={
	types:['string']
}
/* Indicates that this is an interface. */
MyInterface.isInterface=true;

class MyClass extends MyInterface{
	// method (){} // Error: must be present
	method2(arg){
		return 1; // Error: must return [object MyTypeClass]
	}
	//static static_mithod(){}  // Error: must be present
	get react(){
		return 1; // Error: must return string
	}
	// set react (v){} Error: must return
}
MyClass.prototype.prop=1;//  Error: must be string type

/* Generates interface rules and validate class */
InterfaceManager.implementInterfaces(MyClass); 

/* After fixing the errors*/

let myObj=new MyClass();
myObj.method2(1);// Error: argument 1 must be type "string" or "undefined" or "null"
myObj.react=1; // Error: assignment must be a string type.

class MyClass2 {
}
// An alternative way to inherit interfaces.
InterfaceManager.implementInterfaces(MyClass2,MyInterface); // generates errors for the lack of necessary properties
let myObj2=new MyClass2();