Opened 8 years ago

Closed 8 years ago

Last modified 8 years ago

#13442 closed enhancement (worksforme)

Interfaces and Abstract classes - feature request

Reported by: aedart@… Owned by: Eugene Lazutkin
Priority: high Milestone: tbd
Component: General Version:
Keywords: interface, abstract, oop, design pattern, proposal Cc: Kris Zyp
Blocked By: Blocking:

Description

As you all know, JavaScript? does not support Interfaces nor Abstract classes. This "standard" was not approved to be part of the official language, for reasons beyond this proposal. Nevertheless, over the past years both Dojo and other JavaScript? frameworks have accomplished really many great things; allowing front-end developers to work more object-oriented than ever before. But I still feel there is a need for Interfaces and Abstract classes.

Having worked a lot with PHP, I was/am very used to take advantage of Interfaces and Abstract class. So I decided to create my own emulators, which can emulate interfaces and abstract classes, within Dojo's framework. It allowed me to implement my own versions of different kinds of design patterns; Observer, MVC, Composite, Object Pool, etc. Although they do work as I intended them to do, the semantics and how to use them is a bit difficult and their implementation could be a lot better, in terms of performance, correctness, and so on. In other words, it would be nice if Interfaces and Abstract classes could be a native part of Dojo.

Proposal for semantics:
In short, dojo.declare() allows you to define classes. It would be nice to have a dojo.declareInterface() and a dojo.declareAbstract() - methods that could construct objects that simulate the behaviour of an Interface/Abstract? class from, e.g. Java.

In terms of "implementing" an interface, the way I solved this was be class inheritance. However, this is by far the most messy part of my solution. Unless the name of your interface class shows that it is an interface, you do not know what you are implementing. Maybe by creating a dojo.implement() could solve this issue, making it very visible when ever you are implementing interfaces.

Original author of the abstract base classes
It is very important to note, I did not come up with this solution. I was inspired by Medryx Observations (http://blog.medryx.org/?s=javascript+oop). A direct link does not seem to work for the blog entry. However, you will find the abstract base class, if you scroll down.

Other implementations
The solution from Medryx and the one I provided, are far from the only ones on the web. The following links are but a few examples of other developers attempting to create the same thing; http://blog.reinpetersen.com/2008/10/interface-in-javascript.html
http://www.eecho.info/Echo/html/javascript-interface-class/
http://files.trygve-lie.com/examples/js_object_interface/

Future of JavaScript?
I do not know if JavaScript? will ever will support true Interfaces and Abstract classes. However, I do know that a lot of developers wish for such, and that there is a need now.

I really hope that the Dojo community will support this proposal, and that the hard-core JavaScript? developers behind Dojo can create a far better and more suitable solution, than the one I have.

My version of the emulators
The attached rar-file contains the base classes and some few tests. Use them as a point of inspiration - or however it fits you best.

Attachments (1)

dk.rar (14.3 KB) - added by aedart@… 8 years ago.
Base class for Interface and Abstract classes, including some small tests

Download all attachments as: .zip

Change History (4)

Changed 8 years ago by aedart@…

Attachment: dk.rar added

Base class for Interface and Abstract classes, including some small tests

comment:1 Changed 8 years ago by bill

Cc: Kris Zyp added
Owner: set to Eugene Lazutkin

Hmm, well dojo (and javascript) already support interfaces and abstract classes, for example see all the stores defined in dojo.data, which implement the interfaces defined in dojo.data.api (Identity, Notification, Read, Request, Write).

The code you are suggesting is to implement something like java's compile time checking, to throw exceptions like "ItemFileReadStore? forgot to implement method foo from interface bar". In general loose languages like javascript use duck typing rather than strict checking, and I don't think we are interested in changing that, but anyway passing this ticket to Eugene and Kris to close or adopt.

comment:2 Changed 8 years ago by Kris Zyp

Resolution: worksforme
Status: newclosed

As Bill pointed out, Dojo supports mixins which superset the compositional functionality of the concepts you listed, and actually, Dojo already supports type checking as well: http://www.sitepen.com/blog/2009/06/23/unobtrusive-javascript-typing-via-json-schema-interfaces/

comment:3 Changed 8 years ago by aedart@…

Thx for your quick reply. And yes, it is the strict type checking that I miss. However, the article on typing via Json has really caught my attention. Goes beyond what I actually imagined.

Note: See TracTickets for help on using tickets.