In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Today, the editor will share with you the relevant knowledge about what the interface in javascript means. The content is detailed and the logic is clear. I believe most people still know too much about this, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.
In javascript, an interface is a declaration of a series of abstract methods, a collection of method characteristics, and provides a means to indicate which methods an object should have. Interfaces promote code reusability, help stabilize communication between different classes, and reduce problems in the process of inheriting two objects.
The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.
What is an interface?
An interface is a declaration of a series of abstract methods and a collection of method characteristics, which should be abstract and need to be implemented by a concrete class, and then a third party can use this set of abstract method invocations. let the concrete class execute the concrete method.
Interfaces are one of the most useful tools in the toolbox of object-oriented JavaScript programmers. One of the principles of reusable object-oriented design put forward in the design pattern is "programming against the interface rather than implementing it", which is what we call interface-oriented programming.
But the problem is that in the world of JavaScript, there are no built-in methods to create or implement interfaces, and there is no way to judge whether one object implements the same set of methods as another, which makes it difficult to interchange between objects. Fortunately, JavaScript has excellent flexibility, which makes it easy to simulate traditional object-oriented interfaces and add these features.
The interface provides a means to indicate which methods an object should have, and although it can indicate the meaning of these methods, it does not contain a specific implementation. With this tool, you can group objects according to the properties they provide.
For example, if An and B and interface I, even if An object and B object are very different, as long as they both implement the I interface, then An and B can be used interchangeably in the A.I (B) method, such as B.I (A).
You can also use interfaces to develop the commonality of different classes. If you change a function that requires a specific class as a parameter to a function that requires a specific interface as a parameter, then all objects that implement the interface can be passed to it as arguments, so that objects that are not related to each other can be treated in the same way.
Advantages and disadvantages of interface
The established interface is self-descriptive and facilitates code reusability, and the interface can provide information about which methods a class needs to implement. It also helps to stabilize the mode of communication between different classes and reduces the problems in the process of inheriting two objects.
This is also helpful for debugging. In a weakly typed language like JavaScript, type mismatches are difficult to track, and there is a clearer error message if there is a problem with the interface. Of course, the interface is not completely without shortcomings, if a large number of interfaces are used, it will weaken its flexibility as a weakly typed language to a certain extent. On the other hand, JavaScript does not have built-in support for the interface, but only simulates the traditional object-oriented interface, which will make the more flexible JavaScript more difficult to control.
In addition, any way an interface is implemented can have an impact on performance, in part due to additional method invocation overhead. The biggest problem with the use of interfaces is that, unlike other strongly typed languages, JavaScript will fail if it does not abide by the conventions of the interfaces, and its flexibility can effectively avoid the above problems. If it is in a collaborative development environment, its interfaces are likely to be destroyed without any errors, that is, uncontrollable.
In object-oriented languages, interfaces are used in much the same way. The interface contains information that describes the methods that the class needs to implement and the signatures of those methods. Class definitions must explicitly declare that they implement these interfaces, otherwise they will not be compiled.
Obviously we can't do the same in JavaScript because there are no interface and implement keywords, and we don't check whether the interface follows conventions at run time, but we can mimic most of its features through auxiliary methods and explicit checks.
How to implement Interface in javascript
There are three ways to implement an interface in JavaScript:
(1) Annotation description API
(2) attribute detection interface
(3) Duck type discrimination interface
1. Annotation description API: not recommended
Advantages: easy to implement without the need for additional classes or functions.
Disadvantages: pure document constraints, the program cannot check whether the object that implements the interface implements all interface methods
/ * interface Composite {* function a (); * function b (); *} * / / CompositeImpl implements Compositevar CompositeImpl = function () {/ / business logic}; CompositeImpl.prototype.a = function () {/ / business logic}; CompositeImpl.prototype.b = function () {/ / business logic}; 2. Attribute detection API: not recommended
The second method is a little more rigorous. All classes explicitly declare which interfaces they implement, and objects that want to deal with these classes can check against these declarations. Those interfaces are still annotations themselves, but now you can see what interfaces a class claims to implement by examining a property.
Pros: the ability to check which interfaces are implemented
Cons: there is no guarantee that the class actually implements the interface that it claims to implement. All you know is whether it says it implements the interface.
Var interfacesImpl = function () {/ / hold the method name to be implemented in an array inside the implementation class / / usually this property name is defined by the team / / declare that you have implemented both methods, but in fact this.implementsInterfaces = ["Composite", "FormItem"];} / / write a detection function specifically for this implementation object, passing in the instance object to check whether the instance object implements all interfaces function checkImplements (obj) {/ / call the method obj to check whether the two interfaces are implemented, and if not, throw an exception if (! isImplements (obj, "Composite", "FormItem")) {throw new Error ("the interface is not fully implemented!") ;} / / obj is the object to be checked function isImplements (obj) {/ / the 0th parameter passed in is the object to be checked, so check for (var item1; I) from 1
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.