Interfaces in object oriented programming languages and prototype-based languages.
Let's not waste time and get straight to it, what exactly is an Interface to start off with?
From a coding perspective, an Interface, basically refers to programming structure or system that allows two subjects or systems to communicate with one another. An example would be like a user interacting with a device, the device will support a user interface that effectively allows a user to use it to achieve certain goals or objectives.
The interface can only achieve what it is designed for or told to do.
In terms of Object-Orientated Programming(OOP), the interface would refer to all the functions that an object would have to have in order to work properly within it's environment.
Let's head over and look at some of the benefits an interface would offer in Object-Orientated Programming:
Firstly, it allows programmers the opportunity to be quite flexible in referencing Objects. For instance a programmer can use a variable 'var vehicle' to reference any vehicle type like a truck, car, motorcycle etc.
It allows a programmer to describe the relationship between Objects, while with classes, the behaviour as well as the relationship is described.
Interfaces will have objects with the same methods even though, they have nothing in common with their behaviour, it is better practice rather than a situation where you have to extend some class.
So what this means is that it gives a programmer the possibility to design loosely coupled systems, so independent parts of a software can be changed without changing the entire system and still ensuring that the system will operate the same.
Why doesn't JavaScript really make use of interfaces?
Well JavaScript doesn't really offer an interface type even though it is needed at times. JavaScript has a dynamic environment that makes use of prototype-inheritance so it would be difficult to maintain consistency with interfaces across classes.
JavaScript is super dynamic and flexible as it allows a programmer to devine and create Objects, offering various ways to go about creating these Objects.
* One can go about creating a single object, using an object literal. (The easiest way to go about.)
example:
var human = {height:1.8, weight:85, age:50, eyeColor:"blue"};
* One can define and create a single object, using the 'new' keyword.
example:
var human = new Object();
human.height = 1.8;
human.weight = 85;
human.age = 50;
human.eyeColor = "blue";
* One can define an object constructor that aids as a blueprint for all other objects that are created.
example:
var person = {firstName:"Maggie", lastName:"Morrison", age:78, eyeColor:"green"}
var x = person;
x.age = 100; // This allows us to change the age of the person object.
Implementing Interfaces into JavaScript isn't a deadend and can still be done:
I did mention that Javascript offers a flexible environment for programmers and one of the awesome flexibilities it offers is that even though it doesn't really have a built-in way of creating or implementing interfaces, it does offer ways to still implement interfaces. Something known as TypeScript can be used which has a built-in supportive system. It allows us to implement an interface that defines the specifications of an entity and states what needs to be done but doesn't define how it will be done.
The connection between Typescript and Javascript is quite unique. Typescript acts as a layer on-top of JavaScript, it offers the features of JavaScript and adds it's very own layer over JavaScript's layer which is known as the TypeScript type system.
JavaScript's has certain types such as a string, number, object, undefined etc. but there aren't ahead-of-time checks that these are consistently assigned across the database and that's where TypeScript plays a role in acting as this layer.
In other words, the existing JavaScript code is also essentially TypeScript code but TypeScript offers type-checker which indicates which discrepancies you think is happening opposed to what the JavaScript language actually does.
We can also make use of Implement.js to implement interfaces in JavaScript:
Implement.js is a library that we can use by simply installing the package that will assist us as programmers to implement interfaces to JavaScript.
After installing the package, we create a .js file that will allow us to import implement and it's packages.
Have you encountered one of the new features of ECMAScript 5?
JavaScript has a new feature that was added with ECMAScript 5, that allows a programmer to place a program or function in a 'strict' operating context. This can be used to our advantage and be quite handy as it prevents certain actions to be taken and throws more exceptions.
So how do we benefit from this exactly, well the strict mode eliminates a lot of the silent errors by changing them to throw errors visible to the programmer.
The strict mode can fix mistakes that might make it difficult for JavaScript engines to perform optimisations that can promote faster performance.
Strict mode can assist the programmer by preventing certain syntax that could be used in future versions of ECMAScript.
It disables features that are somewhat confusing or poorly throughout which is in place to help us as programmers to write better code that is safer and more secure.
We might experience strict mode as a real pain at times but it certainly helps us to write better code in the long run and which is to our benefit as programmers.
The End...until next time!😉



Comments
Post a Comment