First, what is the interface
Interface is one of the most useful tools in the toolbox of object-oriented JavaScript programmers. One of the principles of reusable object-oriented design in design patterns is the importance of "programming for interfaces rather than implementation", which is what we call interface-oriented programming. The problem is that in the world of JavaScript, there is no built-in method for creating or implementing interfaces, and there is no way to tell whether an object implements the same set of methods as another object, which makes it difficult to use interchangeably between objects, but JavaScript has great flexibility This makes it easy to emulate traditional object-oriented interfaces and add these features. The interface provides a means to illustrate what methods an object should have, although it can indicate the meaning of these methods, but does not include a specific implementation. With this tool, they can be grouped by the attributes provided by the object. For example, if A and B and interface I, even if the A object and the B object are very different, as long as they have implemented the I interface, then in the A.I (B) method can be used interchangeably A and B, such as B.I (a). You can also use interfaces to develop commonalities for different classes. If a function that originally requires a specific class parameter is changed to a function that requires a specific interface as a parameter, then all objects that implement the interface can be passed as parameters to it, so that objects that are unrelated to each other can be treated similarly.
Second, the advantages and disadvantages of the interface
The established interface is self-descriptive and facilitates the reusability of the code, and the interface can provide a message that tells the external class which methods to implement. It also helps to stabilize the communication between different classes, reducing the problem of inheriting two objects in the process. This is also helpful for debugging, where type mismatches are difficult to track in JavaScript, and if there is a problem with the interface, there will be a clearer error message. Of course, the interface is not completely without drawbacks, if a large number of interfaces will weaken its flexibility as a weakly typed language, on the other hand, JavaScript does not have the built-in support for the interface, just the traditional object-oriented interface simulation, This makes JavaScript, which is more flexible, more difficult to navigate. In addition, any way to implement an interface will have an impact on performance, in part due to additional method invocation overhead. The biggest problem with the interface is that JavaScript is not like other strongly typed languages, and if it fails to comply with the interface's conventions, it will fail, and its flexibility can effectively circumvent the above problems, and if it is in a collaborative development environment, its interfaces are likely to be destroyed without any errors, that is, controllability.
In object-oriented languages, interfaces are used in a broadly similar way. The information contained in the interface describes the methods that the class needs to implement and the signatures of those methods. The definition of a class must explicitly declare that they implement these interfaces, otherwise they will not be compiled. Obviously we can't do this in JavaScript, because there are no interface and implement keywords, and we don't check whether interfaces follow conventions at run time, but we can mimic most of their features with helper methods and explicit checks.
Third, imitate the interface in JavaScript
There are three ways to mimic an interface in javascript: through annotations, property checks, and duck-type argument, the above three methods can be combined effectively, resulting in similar interface effects.
Annotations are a relatively straightforward way to simulate an interface by putting the interface-related keywords (such as interface, implement, and so on) together with the JavaScript code in comments to emulate the interfaces, which is the simplest method, but with the worst results.
/************************************************************************************************************** *****************************************************************************************************/
/**
* 创建接口对象 * @param name 接口名 * @param methods 接口方法 */ varInterface = function(name,methods){ if(arguments.length != 2){ thrownewError(‘必须输入两个参数,当前个数‘+arguments.length); } this.name=name; this.methods=[]; for(vari=0, len=methods.length; i<len; i++){ if(typeofmethods[i] !== ‘string‘){ thrownewError(‘方法名参数必须为string‘); } this.methods.push(methods[i]); } }; /** * 接口实现 * @param object1 实现接口对象 * @param object2 对应接口 * @return 实现错误抛出异常 */ Interface.ensureImplements = function(object){ if(arguments.length < 2){ thrownewError(‘必须输入两个参数,当前个数‘+ arguments.length); } for(vari=1, len=arguments.length; i < len; i++){ varinterface = arguments[i]; if(interface.constructor != Interface){ thrownewError("请实现接口"); } for(varj = 0, methodsLen = interface.methods.length; j < methodsLen; j++){ varmethod = interface.methods[j]; if(!object[method] || typeofobject[method] !== ‘function‘){ thrownewError("接口名:"+interface.name+"方法名:"+method+"没找到"); } }; } } var DynamicMap = newInterface(‘DynamicMap‘,[‘centerOnPoint‘,‘zoom‘,‘draw‘]); /** * 执行方法 * @param 函数方法 * @return 执行结果 */ functiondisplayRoute(mapInstance){ Interface.ensureImplements(mapInstance,DynamicMap);//实现接口 /** * 调用 */ mapInstance.centerOnPoint(12,34); mapInstance.zoom(5); mapInstance.draw(); } /** * 函数方法 * @type 实现接口方法 */ vara={ centerOnPoint:function(x,y){ console.log(x*y); }, zoom:function(x){ console.log(x); }, draw:function(){ console.log("x*y"); } } displayRoute(a);
"JS" Interface implementation code