JS Implementation interface

Source: Internet
Author: User

How the interface is implemented

/** * Interface class * @param {String} name Interface name * @param {Array} methods interface method array 
    */function Interface (name, methods) {//check variables, input parameters must be 2                if(Arguments.length!==2) {                    Throw NewError ("State one interface require 2 parameters"); }                 This. Name =name;  This. Methods = []; if(Methods.length <=0) {                    Throw NewError ( This. Name +": Interface method must state"); }                 for(vari =0; i < methods.length; i++) {                    if(typeofMethods[i]!=="string") {                        Throw NewError ( This. Name +": "+ Method[i] +": Interface method must be string"); }                     This. Methods.push (Methods[i]); }            }                        //Create an interface            varComposite =NewInterface ("Composite", ["Add","Delete"]); //classes that implement interfacesfunction Compositeimpl () {} Compositei Mpl.prototype.add=function () {Console.log ("Add ...");                        }; CompositeImpl.prototype.delete=function () {Console.log ("Delete ...");                        }; //declare an instance            varC1 =NewCompositeimpl (); /** * Check method * @param {Object} object*/function Checkclassimplementinterface (Object) {                if(Arguments.length <2) {                    Throw NewError ("Check interface is required 2 parameters"); }                                varImplementinterfaces = {};  for(vari =1; i < arguments.length; i++) {Implementinterface=Arguments[i]; varImplementinterfacemethods =Implementinterface.methods;  for(varj =0; J < Implementinterfacemethods.length; J + +) {                        if(!Object[Implementinterfacemethods[j] | |typeof Object[Implementinterfacemethods[j]]!=="function") {                            Throw NewError (Implementinterface.name +"."+ Implementinterfacemethods[j] +"ID Not method"); }                    }                }            }                        //Check if the class implements all interfacesCheckclassimplementinterface (C1, Composite); 

JS Implementation interface

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.