So today I am reading through this book pro-javascript design patterns, excellent book by the way! I had come upon chapter 2, which was an excellent explanation of interfaces. One thing that concerned myself is that the book states that it is impossible to create enforceable interfaces in javascript. I pondered that for a bit, as far as what interfaces really are. If you look at interfaces from the perspective of java, then for the most part they are types, with a specified subset of methods for a class instance.

Thinking in those terms, I sat back and pondered the problem again, sooo all I need to do really to create interfaces that are enforceable in javascript would be to separate the concept of an interface from a class all together and make it a class its self! WALA! True first class interfaces in javascript!

UML Diagram

So lets see what this new found idea looks like! Here is my interface class that makes it all work.

Interface.js

You can think of interface as linking everything together, but what would a TestInterface look like?

TestInterface.js

Take note, that the interface its self is already implementing Interface.js in a sort-of way! There are two required methods getInterfaceName() and setUpInterface() that need to be implemented otherwise Interface.js will complain loudly!

The basic idea here is that setUpInterface recieves a closure, that is able to access the implementing object, all the TestInterface needs to do is call that method with the same attributes it recieves. It does this by passing in the name of the method into the closure, and then executing the returned function with the same attributes recieved by the method; and return it, SIMPLE!

You can see this happening here:

return inspector(‘testMethod’)(x,y,z);

So now that we have an interface for our class, what does a javascript class have to do to implement this interface?

TestObject.js

The actual implementation of the interface happens here: new Interface(this, new TestInterface());
Wow! thats easy! But what happens when the class doesn’t implement all the methods as this one does?

Well lets find out, if you could place all these scripts in a javascript shell, lets test out a few things.

//Create a new TestObject();
TestThis = new TestObject();

//Lets cast our object to a type of ‘TestInterface’
CastedTestInterface = TestThis.cast(‘TestInterface’);

Don’t take my word for it, here is a FULL WORKING EXAMPLE!

Now if you inspect CastedTestInterface you may notice that it is just an instance of the interface you created! Keep in mind though that it also holds a closure to the object it was casted from.

Lets try executing a method:

CastedTestInterface.testMethod(1,2,3);

Whoa! it worked!
Lets try executing another method specified by the interface:

CastedTestInterface.testMethod2(1,2,3);

Opps, we get an error because this method does not exist! We better fix that, humm but this means that the interface is working!

Long story short, if a method you call from a cast is missing from the object; you will be alerted when you call the method, an error will be thrown, and you scolded for doing such a horrible thing! To be honest, this method is 1/2 of an abstracted class and 1/2 of an interface, an {interstraction}?!?!

This is because most interfaces specify that they can not have implementation details in them, but ours do. On the other hand this is not a abstracted class! Because abstracted classes are extensions of the original, when clearly this type of OOP not an extension, but a subset. Hope you enjoyed the tutorial on how to create interfaces :-) .

  • Reddit
  • StumbleUpon
  • Bebo
  • Yahoo Buzz
  • Delicious
  • Twitter
  • LiveJournal
  • Netlog
  • HelloTxt
  • Share/Bookmark

6 Responses to “Fully Working Javascript First Class Interfaces Are Possible!”

  1. Scott Krutsinger Says:

    Great job on this ad I’m glad to see that there are others extending the realms of OOP JavaScript.

    However…

    I’ve worked on JavaScript interface designs as well. What I found is that the client side performance cost of creating them is too great. Building an object in JSON is the fastest. OOP encapsulation is the slowest and attempting to add an interface makes it even more so.

  2. webactivex Says:

    You are able to do this with static json objects as well. In-fact this code could possibly be optimized even more with a pattern utilizing clone instead! Either way it would probably work excellently!

    var x = {blah:function(){alert(‘wee’);}}
    var p = new Interface(x,new TestInterface());

  3. Tek Says:

    @Scott> Take a look at : http://code.google.com/p/objs/

    It’s a project I have started to better use OOP with Javascript. My library iterates over methods of the interface to check that each is well implemented : http://code.google.com/p/objs/source/browse/trunk/objs/objs_1_0.js#289 at line 289. It’s rudimentary but simple and has proven efficiency on a project I’ve worked on.

  4. webactivex Says:

    Excellent, that method could be used here as well, one of the differences between what I do and a method found in the pro-javascript design patterns (which is the one that you have described) book is that I do not loop through all of the methods (which can be CPU intensive). Instead an actual interface object is created, and passed back, which you end up using at the end of the day as your interfaced object. Though that is not to say that one could not implement such a thing on the example provided.

  5. ????? Says:

    ?????? ???????????? ????? ??????, ??? ???????? ????? ???, ????????????, ?????????? ???????????????? ?????????? ??????? ? ?????. ???????? ????? ??????? ???????????, ????????-????????????, ?????? ??????? ?? ??????? ? ??????? ????? ????????-?????
    ???????, ? ? ??? ???? RSS ????? ? ???? ??????
    “??? ???????? ???? ?????”
    ???????? ?? ????? ???!
    ?????? ?????? ?????, ???????? ?? ?????!

  6. muscle relaxer Says:

    You need think about it. Despite the emails, the overwhelming evidence showing global warming is happening hasn’t changed.
    “The e-mails do nothing to undermine the very strong scientific consensus . . . that tells us the Earth is warming, that warming is largely a result of human activity,” Jane Lubchenco, who heads the National Oceanic and Atmospheric Administration, told a House committee. She said that the e-mails don’t cover data from NOAA and NASA, whose independent climate records show dramatic warming.

Leave a Reply