JavaScript interface implementation code (Interfaces In JavaScript)

Source: Internet
Author: User

In practice, we can define interfaces in annotations and implement them in actual code.
For example: Copy codeThe Code is as follows :/*
Interface Composite {
Function add (child );
Function remove (child );
Function getChild (index );
}
Interface FormItem {
Function save ();
}
*/
Var CompositeForm = function (id, method, action) {// implements Composite, FormItem
...
};
// Implement the Composite interface.
CompositeForm. prototype. add = function (child ){
...
};
CompositeForm. prototype. remove = function (child ){
...
};
CompositeForm. prototype. getChild = function (index ){
...
};
// Implement the FormItem interface.
CompositeForm. prototype. save = function (){
...
};

Do programmers who implement interfaces implement these interfaces? We cannot guarantee it! Because there is no way to check whether all of them are implemented.
We need a mechanism to check whether the interface is implemented, which can be as follows:Copy codeThe Code is as follows :/*
Interface Composite {
Function add (child );
Function remove (child );
Function getChild (index );
}
Interface FormItem {
Function save ();
}
*/
Var CompositeForm = function (id, method, action ){
This. implementsInterfaces = ['composite ', 'formitem'];
...
};
...
Function addForm (formInstance ){
If (! Implements (formInstance, 'composite ', 'formitem ')){
Throw new Error ("Object does not implement a required interface .");
}
...
}
// The implements function, which checks to see if an object declares that it
// Implements the required interfaces.
Function implements (object ){
For (var I = 1; I <arguments. length; I ++) {// Looping through all arguments
// After the first one.
Var interfaceName = arguments [I];
Var interfaceFound = false;
For (var j = 0; j <object. implementsInterfaces. length; j ++ ){
If (object. implementsInterfaces [j] = interfaceName ){
InterfaceFound = true;
Break;
}
}

If (! InterfaceFound ){
Return false; // An interface was not found.
}
}
Return true; // All interfaces were found.
}

This method allows programmers to specify the interfaces implemented during writing: this. implementsInterfaces = ['composite ', 'formitem']; it is theoretically feasible to use the implements method to determine whether it is implemented. It is likely that the 'composite 'interface is implemented, but there is no add method in the code! Therefore, we need to check whether the class implementing the interface contains the methods in the interface! Therefore, the interface must be freed from the Annotations:Copy codeThe Code is as follows: // Interfaces.
Var Composite = new Interface ('composite ', ['add', 'delete', 'getchild']);
Var FormItem = new Interface ('formitem ', ['save']);
// CompositeForm class
Var CompositeForm = function (id, method, action) {// implements Composite, FormItem
...
};
...
Function addForm (formInstance ){
Interface. ensureImplements (formInstance, Composite, FormItem );
// This function will throw an error if a required method is not implemented,
// Halting execution of the function.
// All code beneath this line will be executed only if the checks pass.
...
}

Define the Composite and FormItem interfaces and CompositeForm to implement these two interfaces. When using them, use Interface. ensureImplements to check whether formInstance has implemented all the methods in these two interfaces.
Let's take a look at the Interface Definition: two parameters. The first parameter is the Interface name, and the second parameter is the method array contained in the Interface.Copy codeThe Code is as follows: // Constructor.
Var Interface = function (name, methods ){
If (arguments. length! = 2 ){
Throw new Error ("Interface constructor called with" + arguments. length +
"Arguments, but expected exactly 2 .");
}
This. name = name;
This. methods = [];
For (var I = 0, len = methods. length; I <len; I ++ ){
If (typeof methods [I]! = 'String '){
Throw new Error ("Interface constructor expects method names to be"
+ "Passed in as a string .");
}
This. methods. push (methods [I]);
}
};

Add a recommended static method for the InterfaceCopy codeThe Code is as follows: // Constructor.
Interface. ensureImplements = function (object ){
If (arguments. length <2 ){
Throw new Error ("Function Interface. ensureImplements called with" +
Arguments. length + "arguments, but expected at least 2 .");
}
For (var I = 1, len = arguments. length; I <len; I ++ ){
Var interface = arguments [I];
If (interface. constructor! = Interface ){
Throw new Error ("Function Interface. ensureImplements expects arguments"
+ "Two and above to be instances of Interface .");
}
For (var j = 0, methodsLen = interface. methods. length; j <methodsLen; j ++ ){
Var method = interface. methods [j];
If (! Object [method] | typeof object [method]! = 'Function '){
Throw new Error ("Function Interface. ensureImplements: object"
+ "Does not implement the" + interface. name
+ "Interface. Method" + method + "was not found .");
}
}
}
};

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.