Is there a way to create interfaces in ES6 / Node 4? Is there a way to create interfaces in ES6 / Node 4? javascript javascript

Is there a way to create interfaces in ES6 / Node 4?


Interfaces are not part of the ES6 but classes are.

If you really need them, you should look at TypeScript which support them.


In comments debiasej wrote the mentioned below article explains more about design patterns (based on interfaces, classes):

http://loredanacirstea.github.io/es6-design-patterns/

Design patterns book in javascript may also be useful for you:

http://addyosmani.com/resources/essentialjsdesignpatterns/book/

Design pattern = classes + interface or multiple inheritance

An example of the factory pattern in ES6 JS (to run: node example.js):

"use strict";// Types.js - Constructors used behind the scenes// A constructor for defining new carsclass Car {  constructor(options){    console.log("Creating Car...\n");    // some defaults    this.doors = options.doors || 4;    this.state = options.state || "brand new";    this.color = options.color || "silver";  }}// A constructor for defining new trucksclass Truck {  constructor(options){    console.log("Creating Truck...\n");    this.state = options.state || "used";    this.wheelSize = options.wheelSize || "large";    this.color = options.color || "blue";  }}// FactoryExample.js// Define a skeleton vehicle factoryclass VehicleFactory {}// Define the prototypes and utilities for this factory// Our default vehicleClass is CarVehicleFactory.prototype.vehicleClass = Car;// Our Factory method for creating new Vehicle instancesVehicleFactory.prototype.createVehicle = function ( options ) {  switch(options.vehicleType){    case "car":      this.vehicleClass = Car;      break;    case "truck":      this.vehicleClass = Truck;      break;    //defaults to VehicleFactory.prototype.vehicleClass (Car)  }  return new this.vehicleClass( options );};// Create an instance of our factory that makes carsvar carFactory = new VehicleFactory();var car = carFactory.createVehicle( {            vehicleType: "car",            color: "yellow",            doors: 6 } );// Test to confirm our car was created using the vehicleClass/prototype Car// Outputs: trueconsole.log( car instanceof Car );// Outputs: Car object of color "yellow", doors: 6 in a "brand new" stateconsole.log( car );var movingTruck = carFactory.createVehicle( {                      vehicleType: "truck",                      state: "like new",                      color: "red",                      wheelSize: "small" } );// Test to confirm our truck was created with the vehicleClass/prototype Truck// Outputs: trueconsole.log( movingTruck instanceof Truck );// Outputs: Truck object of color "red", a "like new" state// and a "small" wheelSizeconsole.log( movingTruck );


Given that ECMA is a 'class-free' language, implementing classical composition doesn't - in my eyes - make a lot of sense. The danger is that, in so doing, you are effectively attempting to re-engineer the language (and, if one feels strongly about that, there are excellent holistic solutions such as the aforementioned TypeScript that mitigate reinventing the wheel)

Now that isn't to say that composition is out of the question however in Plain Old JS. I researched this at length some time ago. The strongest candidate I have seen for handling composition within the object prototypal paradigm is stampit, which I now use across a wide range of projects. And, importantly, it adheres to a well articulated specification.

more information on stamps here