What are JavaScript Interfaces?

Before we can delve into that question we need to take a step back and understand what an interface is in programing. JavaScript natively doesn't support interfaces but we can recreate the ideas behind interfaces using Design Patterns. In this article we

Before we can delve into that question we need to take a step back and understand what an interface is in programing. JavaScript natively doesn’t support interfaces but we can recreate the ideas behind interfaces using Design Patterns. In this article we will learn about what is an interface, why it doesn’t exist in JavaScript and how we can create a sudo JavaScript interface.

What are interfaces?

Lately, I’ve been looking for a new apartment in New York to move into. Over the last month i’ve seen a few co-op buildings, got interviewed and more importantly reviewed the buildings Rule books. Interfaces simply put are like co-op rule books. Just like co-up rule books don’t represent actual tenements but the rules that govern them so do interfaces. Interfaces are the rule book for all objects that implement them (or adhere to their rules).

In other words, interfaces do not represent objects but instead they represent the rules that govern objects. In many languages like ActionScript, Java and others, there is a separate definition to these rules (interface). JavaScript has no specific language definition to create interfaces.

Interfaces lists out all the public methods an object must implement. In most object oriented languages there are for types of things you can add to an object: private properties, public properties, private methods and public methods. As such, interfaces only define the rules of public API of objects.

The play interface

Examples:

  1. A DVD remote will enable us to pause, play and change volume.
  2. A Audio system remote will enable us to pause, play and change volume.

For both of these examples the public interface enables us to pause, play and change volume but behind the hood each of these systems are doing completely different things to make that happen. Now imagine that i’m developing a new remote (a global remote) and for our remote to support different types of systems they must have the same interface (so I can call pause, play, volume change in the same way). Luck for us both of these systems have the exact same public interface for these commands (they might have other buttons but they don’t matter to us).

An interface in programing would be like me telling all remotes that want to be supported by my global remote that they must implement 3 methods : play(), pause(), volume(num). That’s exactly what interfaces do – they don’t actually implement the methods but they tell the objects that they must have these public accessible methods and if they don’t an error ensues. If an object didn’t fully match the specs (interface), they cant move in ( usually creating a massive error the stops the execution of the program).

JavaScript Interfaces

Javascript isn’t a strict language and that means it doesn’t have a way to define interfaces. It’s your responsibility, as the developer, to match the virtual interfaces. In languages that don’t have interfaces only when you press the play button ( call the play method) and it doesn’t exist it would generate an error ( in programming that is called a run time error). The more instantiating errors you have the easier it is to debug ( and probably more complicated to build but easier to maintain).

So how to implement JavaScript Interfaces:

That’s where design patterns come into the picture. If you enjoyed this article please join me in my course JavaScript Design Patterns where we will among many other things learn to create JavaScript interfaces. If you want to continue on your own look up Abstract Design Patterns as a good place to continue your research.

Leave a Reply

Be the First to Comment!

Notify of
avatar
wpDiscuz