DEV Community

Cover image for Interfaces in object oriented programming
languages and prototype-based languages
thabang21
thabang21

Posted on

Interfaces in object oriented programming languages and prototype-based languages

Definition of interface in OOP:

interface is a programming structure/syntax that allows the computer to enforce certain properties on an object (class)

Benefits of using interfaces in OOP:

For OOP, using the interface you benefit from: The API is more clear to the user. Implementation detail can be hidden from the user, less confusions and easy swapping between implementations. Unit tests can detect regressions and benefits from mocking. Easier to maintain backward compatibility: keep the legacy interface working while building new ones.

why JavaScript does not really use interfaces :

Interfaces are a contract that defines what classes should look like and JavaScript doesn’t use classes to create objects

How objects are created with JavaScript:

Defining an object:

let object={
name:values
}

Accessing object property:

objectName.propertyName

Example:

var person = {
firstName: "John",
lastName: "Doe",
};

Access by:

person.lastName;

What strict mode is in JavaScript:

Strict Mode is a new feature in ECMAScript 5 that allows you to place a program, or a function, in a “strict” operating context.

Why you would use strict mode is in JavaScript:

JavaScript was designed to be easy for novice developers, and sometimes it gives operations which should be errors non-error semantics. Sometimes this fixes the immediate problem, but sometimes this creates worse problems in the future. Strict mode treats these mistakes as errors so that they're discovered and promptly fixed.

TypeScript definition:

TypeScript is a typed superset of JavaScript ,it makes JavaScript a statically-typed language instead of a loosely-typed language.

how TypeScript can be used to create interfaces:

One of TypeScript’s core principles is that type checking focuses on the shape that values have. This is sometimes called “duck typing” or “structural subtyping”. In TypeScript, interfaces fill the role of naming these types, and are a powerful way of defining contracts within your code as well as contracts with code outside of your project.

How TypeScript enforce strict typing in JavaScript:

TypeScript stands in an unusual relationship to JavaScript. TypeScript offers all of JavaScript’s features, and an additional layer on top of these: TypeScript’s type system.

For example, JavaScript provides language primitives like string, number, and object, but it doesn’t check that you’ve consistently assigned these. TypeScript does.

Reference

Top comments (0)