Is it possible to implement interface in javascript
if yes do you have any example ?
First of all, there is not built in support for traditional abstraction in JavaScript. At least, there are not any types like interfaces and abstract classes. However, interface can be implemented using Object. create method and prototypes.
Interfaces allow for a loosely coupled has-a relationship between classes, similar to composition. Interfaces force a developer to provide their own implementation. Classes can implement multiple interfaces.
Unless the class that implements the interface is abstract, all the methods of the interface need to be defined in the class. An interface can contain any number of methods.
An interface can extend one or multiple existing interfaces. An interface also can extend a class. If the class contains private or protected members, the interface can only be implemented by the class or subclasses of that class.
First of all, there is not built in support for traditional abstraction in JavaScript. At least, there are not any types like interfaces and abstract classes. However, interface can be implemented using Object.create method and prototypes. For more information, visit here.
Quoting from an article written by Mark McDonnell:
In JavaScript there are no true "classic" Object-Oriented features, but through clever usage of the language you can emulate an Interface for use with a JavaScript API.
See the following for the complete article:
http://www.javascriptbank.com/how-implement-interfaces-in-javascript.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With