Interfaces in OOP and Prototype-based Languages

Interfaces in OOP and Prototype-based Languages

This post will focus on learning about interfaces and JavaScript. To cover this, we will review why a prototype-based language like JavaScript doesn’t really use interfaces and how objects can be created using JavaScript. We will also see how interfaces can be emulated using JavaScript and what strict mode is and why you would use it. Finally, we will take a look at what is TypeScript, how it can be used to create interfaces and enforce strict typing in JavaScript. Let’s get started.

Intro to Interfaces

Before we start looking at how JavaScript doesn’t use interfaces and how they can be emulated, we first need to define what it is. Interface in one sentence is a programming structure that allows the computer to enforce certain properties on an object (class) by providing a standard framework in which a set of methods that an instance of a class with that interface could respond to. It allows to design loosely coupled systems through contracts instead of establishing behaviors. This translates into the benefit that you can have objects with the same methods without having the same behavior, a good example is the implementation of the http protocol in different devices. Another benefit is that developers can use abstraction when referencing objects, this allows to hide all the unnecessary details of an object from the user while providing more flexibility in its implementation.

JavaScript and Interfaces

Given JavaScript is a loosely-typed language where it tries to convert data types to allow the user to perform the instruction given, with no built-in ways of creating, implementing interfaces or determining when an object is applying the same set of methods as another object it uses prototypes, which is a constructor function. A constructor type facilitates creating an “object type” like so:

With this, objects of the same type with properties and methods in common can be created. If you want to add new ones you simply add it to the constructor function. Using the prototype property allows to inherit properties and methods from a prototype while adding new properties to object constructors. For the example mentioned above this would mean using the following:

Car.prototype.condition=”new”;

Through the use of a constructor and the prototype property you can assign common methods and properties (sounds familiar?) for those objects created following the steps shown above. This gets you close to having something similar with what you would have in other programming languages where interfaces can be implemented. Knowing the characteristics of interfaces allows some developers to emulate in JavaScript some of the traits they would have with an interface in another language. They could for example force the user through error messages to use an “interface” where all the derived classes have that “interface” such as in the example provided here. They could also fake it with comments to mimic an interface by using the keywords and style of an interface, but since it’s commented out it won’t affect the syntax. One other way is using the package Implement.js that allows to create interfaces to enforce, remove, or rename properties of objects. It can also be used to test the object’s property types. You can find more about Implement.js here.

Strict Mode in JavaScript

Strict mode in JavaScript is a new feature in ECMAScript5 that allows to place a function in a strict operating context where instead of silent errors you have throw errors, it doesn’t allow to use variables or objects that haven’t been declared, you can’t delete a variable or function using “delete”, and keywords reserved for future versions can’t be used as variable names in strict mode. You can see other actions that are affected when using the strict mode here. So why would developers use strict mode if it has so many restrictions? Because it allows to change bad syntax into errors, given how dynamic JavaScript is mistakes could end up turning into variables or errors such as not being indicated when a value was assigned to a non-existing variable.

TypeScript

TypeScript is a strict syntactical superset of JavaScript that enforces strict typing in JavaScript by inferring types that aren’t explicitly declared, and by defining the type of variables and class method parameters it allows the IDE and compiler to spot errors. TypeScript provides features such as classes and interfaces for JavaScript. Interfaces in TypeScript define contracts in the code and check data types. The type checker insures that the properties required by the interface are present and with the stated type. TypeScript also allows the use of optional properties through the use of a “?” sign at the end of the property name when it’s declared:

interface CarData {color?:string;}

Although optional properties sound flexible TypeScript is a strict language, which means that if the user attempts to use a property that was not declared in the interface an error will be thrown. Like in strict mode read-only properties can only be modified when they are created, in TypeScript this is done by defining them as readonly. Another feature of using interfaces with TypeScript is enforcing a class to meet a particular contract and describing methods in an interface that are implemented with the corresponding class:

Image taken from: Source

JavaScript is a dynamic language that works both for the frontend and backend with continuous changes and packages that allow it to have features that it wasn’t meant to have originally; in this case, interfaces. If the developer finds the need to have interfaces he has options such as modules, emulating, or using TypeScript.

Miguel Morales

Leave a Reply

Your email address will not be published. Required fields are marked *