Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement interface in javascript [duplicate]

Tags:

javascript

Is it possible to implement interface in javascript

if yes do you have any example ?

like image 293
user1365697 Avatar asked Jul 17 '12 06:07

user1365697


People also ask

Can we implement interface in JavaScript?

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.

Can class implement multiple interfaces JavaScript?

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.

Can interface implementation have extra methods?

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.

Can an interface extend another interface JavaScript?

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.


2 Answers

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.

like image 71
yusufaytas Avatar answered Oct 08 '22 19:10

yusufaytas


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

like image 41
PraveenVenu Avatar answered Oct 08 '22 18:10

PraveenVenu