JavaScript interface Implementation code (interfaces in JavaScript) _js object oriented

Source: Internet
Author: User
In practice, we can define interfaces in annotations and implement them in actual code.
Like what:
Copy Code code 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 () {
...
};

Does the programmer implementing the interface implement these interfaces? We have no way to guarantee! Because there's no way to check if it's all going to happen.
We need a mechanism to check if the interface is implemented, so you can:
Copy Code code 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 = 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 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 is not found.
}
}
return true; All interfaces were found.
}

This approach allows programmers to specify which interfaces to implement when writing: This.implementsinterfaces = [' composite ', ' formitem ']; In the call to use the implements method to determine whether the implementation, theoretically feasible, it is possible to write the implementation of the ' composite ' interface, but there is no Add method in the code! Therefore, we need to verify that the class that implements the interface contains the methods in the interface! Therefore, the interface must be liberated from the annotation:
Copy Code code as follows:
Interfaces.
var composite = new Interface (' Composite ', [' Add ', ' Remove ', ' 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 would throw an error if a required method is not implemented,
Halting execution of the function.
All code beneath this line is executed only if the checks pass.
...
}

Defines the interface Composite,formitem, and Compositeform implements the two interfaces, which, when used, Use interface.ensureimplements to verify that Forminstance implements all of the methods in both interfaces.
Look at the definition of interface: two arguments, the first argument is the interface name, and the second parameter is an array of methods that the interface contains
Copy Code code 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"
+ "passed in as a string.");
}
This.methods.push (Methods[i]);
}
};

Add a static method for interface whether the proposed interface is implemented
Copy Code code 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" + Interface.name
+ "interface." Method "+ Method +" is not found. ");
}
}
}
};
Related Article

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.