Interface class var Interface = function (name,methods) {if (Arguments.length!= 2) {throw new Error (' The Interface constructor arg
Uments must be 2 length! ');
} this.name = name;
Make sure the elements in the methods are of type string this.methods = []; for (var i = 0,len = Methods.length I <len i++) {if (typeof Methods[i]!== ' string ') {throw new Error (' I
Nterface\ ' methods name must be a string type! ');
} this.methods.push (Methods[i]);
}///Detect if the object implements the method defined in the corresponding interface//If the test does nothing through without doing anything: the browser throws an error interface.ensureimplements = function (object,implinterfaces) {
If the test method accepts a parameter that is less than 2 parameters passed the failure!
if (arguments.length!= 2) {throw new Error (' Interface.ensureimplements method constructor arguments must is = = 2! '); //Get interface Instance object for (var i = 0, len = implinterfaces.length i<len; i++) {var instanceinterface = implinterfaces[i]
; Determines whether the parameter is the type of the interface class if (Instanceinterface.constructor!== Interface) {throw new Error (' The implmented Interface construct
Or not to be Interface Class '); }//Loop interface Instance ObjectEach method for (var j = 0; J < InstanceInterface.methods.length J + +) {//Accept the name of each method with a temporary variable (note is a string) var Methodnam
e = Instanceinterface.methods[j]; Object[key] is the method if (!object[methodname] | | | | typeof object[methodname]!== ' function ') {throw new Error ("The Met
Hod Name ' + methodname + ' is not Found! '}}}
Two: Preparation work://1 Instantiate interface object var compositeinterface = new Interface (' Compositeinterface ', [' Add ', ' remove ']);
var formiteminterface = new Interface (' Formiteminterface ', [' Update ', ' select ']); 2 specific implementation class var Compositeimpl = function () {//Construction-time detection interface.ensureimplements (this,compositeimpl.prototype.implinter
Faces);
}//3 defines the interface to be implemented CompositeImpl.prototype.implInterfaces = [Compositeinterface,formiteminterface];
4 The method to implement the interface implements methods CompositeImpl.prototype.add = function (obj) {alert (' Add ');
Do something ...}
CompositeImpl.prototype.remove = function (obj) {alert (' Remove ');
Do something ...} Compositeimpl.prototype.Update = function (obj) {alert (' Update ');
Do something ...}
CompositeImpl.prototype.select = function (obj) {alert (' select ');
Do something ...}
var C1 = new Compositeimpl (); C1.add ();