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

后端 未结 6 1701
[愿得一人]
[愿得一人] 2021-01-30 08:13

ES6 is fully available in Node 4. I was wondering whether it includes a concept of interface to define method contracts as in MyClass implements MyInterface.

6条回答
  •  误落风尘
    2021-01-30 08:23

    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 cars
    class 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 trucks
    class 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 factory
    class VehicleFactory {}
    
    // Define the prototypes and utilities for this factory
    
    // Our default vehicleClass is Car
    VehicleFactory.prototype.vehicleClass = Car;
    
    // Our Factory method for creating new Vehicle instances
    VehicleFactory.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 cars
    var 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: true
    console.log( car instanceof Car );
    
    // Outputs: Car object of color "yellow", doors: 6 in a "brand new" state
    console.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: true
    console.log( movingTruck instanceof Truck );
    
    // Outputs: Truck object of color "red", a "like new" state
    // and a "small" wheelSize
    console.log( movingTruck );
    

提交回复
热议问题