Interfaces in Object Oriented Programming Languages and Prototype-based Languages
Interfaces in Object Oriented Programming Languages and Prototype-based Languages
Interfaces
An interface defines which methods and properties a class must implement.
Interfaces are a key concept in OOP.
Interfaces specify what a class must do but not how they go about doing it. The interface itself does not contain any code to implement an object. Therefore a class is used to implement an interface, interfaces have to be implemented by a class before you can access them.
Therefore, the interface hides all unnecessary implementation from the user.
Thus one can say that interfaces are used to achieve abstraction, as abstraction involves showing only the relevant data and hiding all unnecessary details of an object from the user.
This is a notable benefit to the use of interfaces as you can use an interface to achieve security i.e. hide certain details and only show the important details of an object (interface).
Another key benefit that software developers use interfaces is to ensure that all classes are built according to rules, or contracts, outlining what methods and properties must be implemented.
An interface is often referred to as a contract because it is agreed that a class that implements the interface will implement the properties and methods defined in the interface.
Further, using interfaces is also a way of enforcing strict typing to a certain degree.
For example, Python supports the implementation of interfaces because it is an OOP language.
JavaScript
On the other hand, JavaScript has no built-in way of creating or implementing interfaces.
It also lacks built-in methods for determining whether an object implements the same set of methods as another object, making it difficult to use objects interchangeably.
In JavaScript, almost "everything" is an object.
- Booleans can be objects (if defined with the
new
keyword) - Numbers can be objects (if defined with the
new
keyword) - Strings can be objects (if defined with the
new
keyword) - Dates are always objects
- Maths are always objects
- Regular expressions are always objects
- Arrays are always objects
- Functions are always objects
- Objects are always objects
There are different ways to create new objects in Javascript.
There are different ways to create new objects:
- Create a single object, using an object literal.
- Create a single object, with the keyword
new
. - Define an object constructor, and then create objects of the constructed type.
const person = {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"};
const person = new Object();person.firstName = "John";person.lastName = "Doe";person.age = 50;person.eyeColor = "blue";
function Person(first, last, age, eyecolor) {this.firstName = first;this.lastName = last;this.age = age;this.eyeColor = eyecolor;this.nationality = "English";}
Since interfaces are a contract that defines what classes should look like and JavaScript doesn’t use classes to create objects this makes sense.
It is for this reason that Javascript has no built-in way of creating or implementing interfaces.
However, JavaScript is very flexible and there are several ways in which we can achieve the goals of interfaces using JavaScript.
One could emulate interfaces using JavaScript by use of:
- comments,
- attribute checking, and
- duck typing.
No single technique is perfect, but a combination of all three will come close.
In the end, it doesn't matter whether a class declares the interfaces it supports, as long as the required methods are in place. That is where duck typing comes in. Duck typing was named after the saying, “If it walks like a duck and quacks like a duck, it's a duck.” It is a technique to determine whether an object is an instance of a class based solely on what methods it implements, but it also works great for checking whether a class implements an interface. The idea behind this approach is simple: if an object contains methods that are named the same as the methods defined in your interface, it implements that interface.
Strict mode can be used in global scope for the entire script, by adding the string "use strict" at the beginning of your script or it can be applied to individual functions, by adding the string "use strict" in your functions.
Strict mode makes several changes to normal JavaScript semantics:
- Eliminates some JavaScript silent errors by changing them to throw errors.
- Fixes mistakes that make it difficult for JavaScript engines to perform optimisations: strict mode code can sometimes be made to run faster than identical code that's not strict mode.
- Prohibits some syntax likely to be defined in future versions of ECMAScript.
Strict mode makes it easier to write "secure" JavaScript. It changes previously accepted bad syntax into real errors.
Therefore, strict mode reduces bugs, improves security and overall performance of your application.
TypeScript
TypeScript is designed by Microsoft and compiles into plain JavaScript. TypeScript is a typed superset of JavaScript. TypeScript makes JavaScript a statically-typed language instead of a loosely-typed language.
Statically typed languages, you have to specify what data type a variable will contain when you declare it. If you declared a variable as a string in a statically typed language and later try to assign that variable a value that is a number, it would throw an error.
However, with dynamically typed languages, object types are determined at runtime. Therefore, you can declare a variable that can hold a string at one point in time but a number at another stage
Therefore TypeScript allows for the usage of interfaces. As we know from above, an interface adds the functionality of strong type checking for your functions, variables, or the class that is implementing the interface. Thus we can see how TypeScript makes JavaScript a statically-typed language.
One of TypeScript’s core principles is that type checking focuses on the shape that values have. The TypeScript compiler does not convert interface to JavaScript but it uses interface for type checking. This is sometimes called “duck typing” as we have seen above in emulating interfaces for JavaScript!
Overall TypeScript provides a way to describe the shape of an object, better documentation and validation to check whether your code is working as expected.
Comments
Post a Comment