Interface interface usage in JavaScript

Source: Internet
Author: User
Tags constructor static class

But there is no corresponding mechanism in JavaScript, but JavaScript is very flexible, we can use its features to imitate interface, but we need to add some methods to do check action

We can still use programs to simulate the implementation of JavaScript interface interfaces. In general, the implementation of the simulation interface has the following three ways:

1. Annotation Method--the definition of the interface is written in the annotation, can you be careful to realize that all the methods of the interface are all based on conscious
2. Attribute check Method-------------------------------------------------------------
3. Duck-type discrimination--like a duck, a quack is called a duck. What we can call a duck is something that has the characteristics of a duck. All but the robots that can walk upright and use tools are human. Well ~ ~

According to the book, we also have the same color annotation method and duck type discrimination method, to simulate the interface implementation of JS.

The code is as follows Copy Code

//Constructor
/*
 * @param name of String interface
 * @param methods Array Interface The method defined inside
*/
var in Terface = function (name, methods) {
   //If the parameter of the purchase function is not equal to 2, throw an exception
    if ( Arguments.length!= 2) {
        throw new Error ("Interface constructor called With "+ Arguments.length +
       " Arguments,but expected Exactyl 2. ")
   }

THIS.name = name;
This.methods = [];
Method array, which guarantees that each element is a string type in the methods array that is passed in.
for (var i = 0, len = methods.length i < len; i++) {
if (typeof Methods[i]!== "string") {
throw new Error ("Interface constructor expects methods names to Bo" +
"Passed in asastring.");
}
This.methods.push (Methods[i]);
}
}

Static class Methods
Interface.ensureimplements = function (object) {
If there are fewer than 2 arguments, an exception is thrown, object is the objects to be judged to implement the interface
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++) {
Inter_face as an interface, be sure to implement the interface class
The book uses interface, because it is a reserved word in JavaScript, so temporarily replace it with Inter_face
var inter_face = arguments[i];
if (inter_face.constructor!== Interface) {
throw new Error ("Function interface.ensureimplementsexpects arguments" +
"Two and above to be instances of Interface.");
}
for (var j = 0, Methodslen = inter_face.methods.length J < Methodslen; J + +) {
object contains methods defined in the interface
var method = Inter_face.methods[j];
if (!object[method] | | | typeof object[method]!== ' function ') {
throw new Error ("Function Interface.ensureImplements:object" +
"Does not implements" +
Inter_face.name +
"Interface. Method "+
Method +
"is not found.");
}
}
}
}

Perhaps everyone is more dizzy, where is the note written? A note is written on the calling page. For example:

The code is as follows Copy Code

Define interface composite, implement three methods of Add,remove,getchild
var composite = new Interface (' Composite ', [' Add ', ' Remove ', ' getchild ']);

Define interface Formitem, implement Save method
var formitem = new Interface (' Formitem ', [' Save ']);

Determine if the object implements the two interfaces above
var object = new Class ();
Interface.ensureimplements (Object,composite,formitem);

Interface is easy to use, but sometimes it is necessary to determine whether the interface. It's a hard job. Concrete analysis of the specific situation, the decision right in you.

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.