title

Interface In Js : A Programming Tutorial To Create A Simple Interface In Js

The interface acts as a contract defining how behavior can be changed. Hence, interfaces are used by various libraries, frameworks, and tools.

An interface is a contract between client code and an implementation that describes a set of methods, properties, or other features. It serves as an abstraction between two classes (or objects) by providing common behavior for all clients and eliminating unnecessary details from each class’s implementation.

What Is Interface

The interface is a set of methods or properties that define the behavior of an object. A class can implement more than one interface, and each implementation will have its own set of methods. For example, a Person class could be implemented in both Serializable and Comparable interfaces to ensure that it conforms with their requirements.

Interfaces can contain fields and methods, but not properties while implementing an interface, all methods that are defined in the interface must be implemented. You can’t provide your implementation of the method, but you can override it with a different implementation.

What Is Interface in JS

An interface is a set of methods and properties that can be used to define the behavior of an object. Interfaces are used to define the contract between an object and the code that uses it. Interfaces are stated employing the interface keyword:

interface MyInterface {

fetchData(); }

The syntax for declaring an interface is similar to that of a class, but with a couple of important differences. First, the keyword interface must be used instead of class Second, interfaces don’t have any implementation code—the methods and properties are declared using only their names and return types.

An interface is a set of methods, properties, and constants that can be implemented by classes. They are used to define the contract between a class and its users, which means that classes must implement this contract in order to use certain functions or methods from an interface.

For example, you may have defined an object called Counter in JavaScript which has two methods (increment() and decrement()) that allow you to add or subtract 1 from its value respectively. You could then use this object as follows:

var counter = new Object(); // create instance

counter.increment(); // increase by 1

counter.decrement(); // decrease by 1

What are the types of Interface in JavaScript?

In JavaScript, there are three types of interfaces:

Function Interface

A function has a name and a list of parameters. It is defined in a program.  A function-based interface defines a method that can be invoked with the name of “call”. This type of interface is used to define functions, however, it does not support any other methods than those defined by its parent class.

Object Interface / Class Interface

A class has a function name, attributes, and methods. A new class can be instantiated from another class or directly by using a new keyword. An object-based interface defines a property as seen in the following figure:

  • (object) { }
  • {x();}

Prototype Interface

An object created using the new keyword has both a constructor and prototype property that implements the methods of the interface which is inherited from the Object # prototype object.

It may seem like a lot at first, but once you get the hang of it, it can be really useful for organizing your code and keeping track of what’s going on within your application.

In the previous section, we saw how functions can be used to define methods and properties that can be used to access a value. The interface as a function type enables users to write code that uses the object returned by an instance of this interface.

An Interface As Function Type (IAFT) is a special type of function, which takes one or more arguments and returns an object (or IDs). An IAFT can be used in the following ways:

It can be used when defining an interface with only generic types, like string or int; these types will be matched against each other when they are passed into IAFTs. This allows us not only to create interfaces but also to contribute new functionality to existing ones without changing any code at all!

Creating An Interface

  • Create a new file, interface.js, and name the interface.
  • Create a new class and name it Interface.
  • Insert the below code into the Interface class:

“`javascript var Interface = { constructor() { console.log(“Hello world!”); },

sayHello(name1) { console.log(“Hello world!”, name1); } } “`

  • Import the following libraries: `react`, `react-dom`, and `react-native`.
  • Add the following code to your index.js file: 

“`javascript import React from ‘react’;

import ReactDOM from ‘react-dom’; import { AppRegistry } from ‘react-native’; 

import Interface from ‘./interface’; ReactDOM.render(<Interface />);

 AppRegistry.registerComponent(‘com.example’, () => Interface);

“interface Foo { function getNumber(): number;

function getString(): string; } class Bar implements Foo { function getNumber(): number { return 42; } function getString(): string { return ’42’; }`

class Bar implements Foo { function getNumber(): number { return 42; } function getString(): string { return ’42’; }“

Sample Program To Explain Interface In Js

The following is a sample program to explain the concept of interfaces in JavaScript:

  • A class can have multiple interfaces. For example, if you want your class to be able to manipulate objects and arrays, it needs an interface that handles those operations.
  • An object does not have any interface by default; however, you can create one by using the keyword interface:

“`js var obj = {}; // A normal object var myObj = {}; 

// Another normal object 

var myInterface = {};

// A custom type definition (interface) “`

The syntax for defining an interface is similar to that of a class definition. You can declare the name, any methods and properties it contains, its constructor (if required), and so on: 

“`js interface MyInterface { // Interface definition goes here } “`

Conclusion

As you can see, declaring an interface is not difficult. You just need to know the syntax and how it differs from that of a class definition. I hope this article helped you understand what interfaces are and when (and why) they should be used.

The key takeaways from this article are that classes are for creating objects, and interfaces are for defining custom types and their behavior in JS. Classes can have constructors and methods, but not properties or fields; interfaces can have all four. We at CognitionTeam provide custom applications for organizations and are using Javascript with react native so do contact us if you have any questions!

Read more regarding JS and additional programming languages in the following blogs:

  • Flutter vs React Native
  • React vs React Native
  • Next Js vs React Js
  • React Native SVG
  • 7 Crazy JS features
  • 5 Core JS using Frameworks
  • Java vs JavaScript
  • Java Composite Data Types
  • Importing in JS
avatar

Shubham Singh

published: December 9, 2022