Interface
And Class
, JavaScript does not have native support for interfaces.
Here are three ways to implement the interface in the book:
|
Tips |
In the real world, we're going to make a choice between the books. |
-
comments
Annotation uses documents to constrain code, not enforce constraints, but only by the developer's conscious maintenance. The advantage of
Is that it does not support additional code, does not increase the file size, and does not affect execution speed.
The disadvantage is that you cannot provide a check, error message.
# interface composite# Add (Child) # Remove (Child) # Getchild (index) # # INT Erface formitem# Save () Compositeform = (ID, method, action), # implements Composit, FormItem <link xmlns= "h Ttp://docbook.org/ns/docbook "xlink:href="%e5%af%8c%e6%9c%89%e8%a1%a8%e7%8e%b0%e5%8a%9b%e7%9a%84javascript.xml " xmlns:xlink= "Http://www.w3.org/1999/xlink"/> Compositeform.portotype = Add:-------... getchild:---
Property Check
This method can check if an object is annotated to implement some interfaces, but does not check whether the method that implements the interface definition is implemented.
# interface composite# Add (Child) # Remove (Child) # Getchild (Index) # # interface formitem# Save () Compositeform = (ID, method, action), @implementsInterfaces = [' Composite ', ' FormItem '] ... addform = ( forminstance), if (!implements (forminstance, ' Composite ', ' FormItem ')) throw new Error (' Object does not Implements a required interface. ') ... # Add to Global/windowglabol.implements = (object), for i in [1...arguments.length] # interfaces if not Arguments[i] in object.implementsinterfaces return false True
Duck Type Distinguishing form
This method, on the previous basis, examines whether all the methods that implement the interface are implemented. And a new class is added to assist in the design of the Interface
interface.
# interfacescomposite = new Interface ' Composite ', [' Add ', ' Remove ', ' getchild ']formitem = new Interface ' FormItem ', [' Save ']# classcompositeform = (id,method,action) ... # implements# Business Codeaddform = (forminstance)-Interf Ace.ensureimplements forminstance, Composite, formitem# Interface classinterface = (name, method), throw new Error ( "Interface constructor called with #{arguments.length} arguments, but expected exactly 2.") If arguments.length!=2 @name = name @methods = [] For method in methods throw new Error (' Interface con Structor expects method names to being passed in as a string. ') If typeof Method! = ' string ' @methods. Push method # static Class Methodinterface.ensureimplements = (object) throw new Error ("expected at least 2 arguments") if arguments.length<2 for interface in ARGUMENTS[1...A Rguments.length] throw new Error ("Function interface.ensureimplements expert argumnets" and above to IS inStances of Interface. ") If interface.constructor! = Interface for method in Interface.methods if!object[method] | | typeof Object[method]! = ' function ' throw new Error ' object does not implemnet the #{interface.name} interf Ace. Method #{method} is not found. "
|
Tips |
If the above interface implementation is used, then the type checking (instanceof) in the code should be removed because we have directly checked the implementation of the method. |
Reread the JavaScript design pattern-Interface