Interfaces in object oriented programming languages and prototype-based languages
Interfaces and the benefits of using them in OOP
Interfaces are seen as contracts implemented in Object Orientated Programming because they are the entities which declare which methods and properties a class should have however, do not contain any code specifying how the methods will be executed. When using an OOP language like Python which is a strongly-typed language, interfaces are important for ensuring consistency in how classes are built and to enforce strict typing.
Does JavaScript use interfaces?
JavaScript is a prototype-based language that works with objects however they are not created through classes. Prototypes are used instead therefore JavasSript does not use interfaces. A prototype is a constructor function as shown below:
Above we see how the object mySong is created following the blue prints declared by the constructor.
First the the firstSong object is created with parameters passed to the constructor and then the method announceSong is called through the new object.
How can JavaScript emulate interfaces?
Though JavaScript does not have a built in method of implementing interfaces, they can be emulated in the following ways:
- Using Comments - where you can imitate an interface through declaring keywords in comments. This is the most simple method however it does not emulate interfaces effectively as complying to the interface is optional and there is no way of checking if the "contract" is really being followed.
- Using Attribute Checking - where classes declare interfaces they use and these declarations are verified by the objects interacting with those classes. The interfaces are still just in comments but attributes can be checked to see the interfaces a class claims to use and an error will be thrown if it does not comply. The drawbacks with this method is that there is no certainty given that a class truly complies with an interface but can only know that it claims to comply and not know whether it implements the required methods or not.
| Example of Attribute Checking: https://jscriptpatterns.blogspot.com/2013/01/javascript-interfaces.html |
- Using Duck Typing - where comments are not used like the other two methods mentioned above. It checks the names of the methods used in a class to see if they match the names of the methods declared in the interface. It contains a helper function that takes an object to be checked and the interfaces it will be compared with as arguments. This gives opportunity for an error message to be thrown when checking an object, which are very useful, containing information about the missing method and the interface which has been implemented.
| Example of the comments & duck typing combo: https://jscriptpatterns.blogspot.com/2013/01/javascript-interfaces.html |
Strict Mode
For a more safer feature of JavaScript strict mode can be used. It is a method launched by ECMAScript 5 to allow functions or entire scripts to switch to a more restricted version of JavaScript that prevents problematic actions from being taken and throws more exceptions as errors. It is known to have the following benefits:
- JavaScript silent errors or accepted mistakes are eliminated and converted to throw errors.
- With strict mode your code can be executed faster than code that is not strict mode.
- Forbids syntax that will most probably be defined in future ECMAScript versions.
- Creates ease in creating more secure JavaScript code
More information on how to implement strict mode to your scripts, functions, modules through the "use strict" statement click on this link
TypeScript
TypeScript provides a strongly typed version of JavaScript that uses classes and interfaces. It is beneficial for highlighting unexpected behavior occurring in your code and reducing the chances of bugs occurring. Typescript uses Duck Typing where the interface is used to determine the shape that values have and used as a reference to check if the terms of the contract are adhered to. It can be used to enforce strict typing and interfaces in the following ways:
Interfaces
- Interfaces can be implemented in TypeScript in a very straight forward manner. An interface is declared then an object is created according to that interface as shown in the image below:
- A Type alias can be used to to accomplish the same thing too:
Strict Checks
There are many features to TypeScript's strict checks that enforce strict typing such as alwaysStrict, noImplicitAny, noImplicitThis, strict, strictBindCallApply, strictFunctionTypes, strictNullChecks and strictPropertyInitialization. We will go through alwaysStrict, strictNullChecks and strictBindCallApply however all are covered in detail here.
- alwaysStrict - with this feature 'use strict' is implemented on all files and you can be ensured that all documents are parsed in ECMAScript strict mode.
- strictNullChecks - ensures that null/undefined/false is set to be not ignored and can be returned if the variable called is empty and where a value is expected as shown in the image below.
| Example of strict null checks: https://www.typescriptlang.org/tsconfig |
- strictBindCallApply - ensures the correct arguments are passed for built-in methods of functions such as call, bind and apply are used. In the image below we see how the function is set to have a value passed with a string as arguments and when the requirements are not being met (as seen in the last line of code) an error is shown.
| Example of strictBindChecksApply: https://www.typescriptlang.org/tsconfig |
In conclusion to we see how TypeScript is a great solution for developers who don't want to shift away from JavaScript however want to write more secure and code that's not sloppy. The drawbacks is it is not always easy to convert JavaScript code to TypeScript therefore the best option is to start a new project using TypeScript or use strict mode incrementally and if converting JavaScript becomes complicated.
References:
Budinoski, K., 2013. Javascript
patterns. [Online]
Available at: https://jscriptpatterns.blogspot.com/2013/01/javascript-interfaces.html
[Accessed 16 June 2021].
HyperionDev, 2021. Interview Preperation:
Interfaces, s.l.: s.n.
Microsoft, 2021. Interfaces. [Online]
Available at: https://www.typescriptlang.org/docs/handbook/interfaces.html
[Accessed 16 June 2021].
Microsoft, 2021. Intro to the TSConfig Reference. [Online]
Available at: https://www.typescriptlang.org/tsconfig
[Accessed 18 June 2021].
Microsoft, 2021. TypeScript for JavaScript
Programmers. [Online]
Available at: https://www.typescriptlang.org/docs/handbook/typescript-in-5-minutes.html
[Accessed 16 June 2021].
Mozilla, 2021. Constructor. [Online]
Available at: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/constructor
[Accessed 16 June 2021].
Mozilla, 2021. Strict Mode. [Online]
Available at: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode
[Accessed 16 June 2021].
Comments
Post a Comment