JS is an object-oriented language, but JS also lacks many object-oriented features, such as inheritance, there is no interface inheritance and no parent class inheritance, so sometimes it is necessary to implement the inheritance of human.
First, look at the object-oriented inheritance in Java:
//define the flight movements of birds of the classInterfacebirdflyable { Public voidfly ();}//BirdsclassBirdImplementsbirdflyable{ Public voidFly () {System.out.println ("Bird is flying"); }}//bats can also fly, but they do not inherit the movements of birds.classbianfu{ Public voidFly () {System.out.println ("Bianfu is flying"); }} Public classtest{//define the Fly method, which requires an incoming bird flight method Public voidFly (birdflyable flyable) {flyable.fly ()} Public Static voidMain (string[] args) {Bird Bird=NewBird (); Fly (bird);//Bird is flying//bianfu bianfu=new Bianfu (); Fly (BIANFU)//Execute Bat method, error }}
Here you can see fly (bird) execution results of bird is flying, if Bird inherited birdflyable but did not implement the Fly method, will be at compile time error, on the contrary, if using fly (BIANFU), because the bat does not inherit the interface, the same will be error, As a result, interfaces in Java inherit from strict inheritance.
Ii. python inheritance
#defining bird Flight methodsclassbirdflyable ():defFly (self):Pass#Defining birdsclassBird (birdflyable):defFly (self):Print('Bird is flying')#Defining BatsclassBianfu ():defFly (self):Print('Bianfu is flying')#increased method of hope for incoming bird flight methodsdeffly1 (birdflyable): Birdflyable.fly ();#It turns out that the incoming bat is also possible.Fly1 (Bird ())#Bird is flyingFly1 (Bianfu ())#Bird is flying
In Python, because the parameters in the method do not have a specified type, the function can be completed as long as the execution contains this method.
This is the "duck type" of dynamic language, it does not require strict inheritance system, an object as long as "look like a duck, walk like a duck", it can be regarded as a duck.
Three, JS to implement the interface
Currently there are three ways to implement the interface in JS:
1, document comments, that is, pre-written in the document comments the interface, the method in the interface, the developer according to the comments to write the interface, this method is not very useful to force developers to inherit the interface.
2, attribute detection method: When defining the class, specify which interfaces the class inherits from, this method can only detect the interface name, cannot detect the concrete implementation method.
3, Duck type method: As in Python, the source and "look like a duck, walk like a duck", then it can be regarded as a duck theory, focusing on what the class implementation of the method, and ignore the inheritance of that interface, to determine the inheritance of the class, the implementation method is as follows:
//the best way to implement JavaScript interface by Duck type argument //The core approach is to detect if every method of the interface is implemented //object-oriented, unified code, and decoupled //1. Interface class /** * Interface requires two parameters, 1, first name, 2, abstract method name array*/ varInterface=function(name,methods) {//throws an exception if the argument is greater than one and the second argument is an array if(arguments.length>=2&& (arguments[1)instanceofArray)) { This. name=name; This. methods=[]; for(varIndexinchmethods) { if(typeofmethods[index]== ' String '){ This. Methods.push (Methods[index]); }Else{ Throw NewError (' Method name should be string ')) } } }Else{ Throw NewError ("interface requires two parameters, 1, name, 2, array of abstract method names"); } } //define two interfaces, respectively, to delete and change the search varinterface1=NewInterface (' Interface1 ', [' Add ', ' delete ']); varInterface2=NewInterface (' Interface2 ', [' Modify ', ' query ']); /** * Define the interface test class, the main test is whether to implement all the methods in the interface * interface passed parameters are: 1, need to detect the class, 2, other interface parameters*/Interface.checkinterface=function(object) {if(arguments.length>=2){ for(vari=1,len=arguments.length;i<len;i++){ //get the interface you want to detect varInterface=Arguments[i]; //get all the methods contained in the interface for(varj=0,methods=interface.methods;j<methods.length;j++){ varMethod=Methods[j]; if(!object[method]| | (typeofobject[method]!= ' function ')){ Throw NewError ("method" +method+ "not Implemented"); } } } }Else{ Throw NewError (' Interface validation error, parameter at least two bits ')) } } //defining the From class //implementing the methods in Interface 1 and interface 2 functionForm () { This. add=function() {alert (' Add '); }; This.Delete=function() {alert (' Delete ') }; This. modify=function() {alert (' Modify ') }; This. query=function() {alert (' Query ') } } //Instantiating Classes varform =NewForm (); //make an excuse to checkinterface.checkinterface (Form,interface1,interface2) Form.add ();
[Technical Learning]js Interface inheritance