INTRODUCTION
The aim of this blog is to explain what an interface is and the benefits of using interfaces in OOP, moreover expand on why JavaScript does not really use interfaces and how objects are created with JavaScript, and discuss how one could emulate interfaces using JavaScript, shed light on what what strict mode is in JavaScript and why one would use this. Furthermore, expand on the idea of TypeScript is and how it can be used to create interfaces and enforce strict typing in JavaScript.
Interfaces in Object Oriented Programming Languages
An interface is a description of the actions that an object can do... for example when you flip a light switch, the light goes on, you don't care how, just that it does. In Object Oriented Programming, an Interface is a description of all functions that an object must have in order to be an "X". Again, as an example, anything that "ACTS LIKE" a light, should have a turn-on() method and a turn-off() method. The purpose of interfaces is to allow the computer to enforce these properties and to know that an object of TYPE T (whatever the interface is ) must have functions called X,Y,Z, etc.
Why JavaScript does not really use interfaces and how objects are created with JavaScript
Interfaces are not a thing in JavaScript, not really anyway. JavaScript is a dynamic language, one where types are changed so often that the developer may not have even realised, because of this people argue there is no need for an interface to be added to the ECMAScript standard that JavaScript is based on.
However, JavaScript has grown massively as a backend language in the form of Node.js, and with that comes different requirements and a different crowd who may have a different opinion. To add to this, the language is quickly becoming the front-end tool; it has got to the point where many developers will write the vast majority of their HTML inside .js files in the form of JSX.
So as the language grows to take on more roles, it is helpful then to make sure one of our most crucial data structures is what we expected it to be. JavaScript may have the class keyword, but in truth this is just an uninstantiated constructor function, and once called it is simply an object. Objects are ubiquitous, so it is sometimes beneficial to ensure they match a specific shape.
Recently at work I found a case where a developer was expecting a property returned as part of an API response to be true but instead got "true", causing a bug. An easy mistake, and one that could also have been avoided if we had an interface.
How you could emulate interfaces using JavaScript
In Object-Oriented programming languages, an interface defines a set of methods which a Class must include in order to implement the interface (otherwise, if the Class is missing the required methods, the code will fail and the interface will throw an error).
Interfaces are useful for making sure developers use the correct implementation of an API.
In JavaScript there are no true "classic" Object-Oriented features, but through clever usage of the language you can emulate an Interface for use with a JavaScript API. In this free HTML JavaScript tutorial, Mark McDonnell guides you how to implement Interfaces in the JavaScript programming language.
In Object-Oriented languages an interface defines a set of methods which a Class must include in order to implement the interface (otherwise, if the Class is missing the required methods, the code will fail and the interface will throw an error). Interfaces are useful for making sure developers use the correct implementation of an API. In JavaScript there are no true 'classic' Object-Oriented features, but through clever usage of the language you can emulate an Interface for use with a JavaScript API.
Strict mode in JavaScript
- Strict Mode was a new feature in ECMAScript 5 that allows you to place a program, or a function, in a “strict” operating context. This strict context prevents certain actions from being taken and throws more exceptions. The statement “use strict”; instructs the browser to use the Strict mode, which is a reduced and safer feature set of JavaScript.
Benefits of using use strict: Strict mode makes several changes to normal JavaScript semantics.
- Strict mode eliminates some JavaScript silent errors by changing them to throw errors.
- Strict mode fixes mistakes that make it difficult for JavaScript engines to perform optimizations: strict mode code can sometimes be made to run faster than identical code that’s not strict mode.
- Strict mode prohibits some syntax likely to be defined in future versions of ECMAScript.
- It prevents, or throws errors, when relatively “unsafe” actions are taken (such as gaining access to the global object).
- It disables features that are confusing or poorly thought out.
- Strict mode makes it easier to write “secure” JavaScript.
How to use strict mode: Strict mode can be used in two ways, remember strict mode doesn’t work with block statements enclosed in {} braces.
Used in global scope for the entire script.
It can be applied to individual functions.
Using Strict mode for the entire script: To invoke strict mode for an entire script, put the exact statement “use strict”; (or ‘use strict’;) before any other statements.
// Whole-script strict mode syntax
let v = "strict mode script!";
This syntax has a flow, it isn’t possible to blindly concatenate non-conflicting scripts. Consider concatenating a strict mode script with a non-strict mode script. The entire concatenation looks strict, the inverse is also true. The non-strict plus strict looks non-strict. The concatenation of strict mode scripts with each other is fine, and concatenation of non-strict mode scripts is fine. Only concatenating strict and non-strict scripts is problematic. It is thus recommended that you enable strict mode on a function-by-function basis (at least during the transition period).
What TypeScript is and how it can be used to create interfaces
An Interface is a structure which acts as a contract in our application. It defines the syntax for classes to follow, means a class which implements an interface is bound to implement all its members. We cannot instantiate the interface, but it can be referenced by the class object that implements it. The TypeScript compiler uses interface for type-checking (also known as "duck typing" or "structural subtyping") whether the object has a specific structure or not.
The interface contains only the declaration of the methods and fields, but not the implementation. We cannot use it to build anything. It is inherited by a class, and the class which implements interface defines all members of the interface.
When the Typescript compiler compiles it into JavaScript, then the interface will be disappeared from the JavaScript file. Thus, its purpose is to help in the development stage only.
Interface Declaration
We can declare an interface as below.
- An interface is a keyword which is used to declare a TypeScript Interface.
- An interface name is the name of the interface.
- An interface body contains variables and methods declarations
Example
In the above example, we have created an interface OS with properties name and language of string type. Next, we have defined a function, with one argument, which is the type of interface OS.
Now, compile the TS file into the JS which looks like the below output.
Output
Use of Interface
We can use the interface for the following things:
- Validating the specific structure of properties
- Objects passed as parameters
- Objects returned from functions.
Comments
Post a Comment