Code Monkey home page Code Monkey logo

es6-interface's Introduction

es6-interface

es6-interfac is a Nodejs module that let you build interfaces for es6 classes

Introduction

in today javascript there is no way to declare an interface like other language's as php or java. with es6-interface easily build interfaces , use multi interfaces on the same class and secure your class.

Limitations

works on NodeJS version 6+, works only on modern web browsers, tested only on NodeJS

Installing

npm install es6-interface --save

lets jump to some examples

Examples

first we start with a simple 1 interface implementation

One interface implementation

const Interface = require('es6-interface')
const testInterface1 = new Set(['required1(arg1)']); // required1 is the method we force to implement

class testClass extends Interface(testInterface1) {
  constructor() {
    super()
  }

}

new testClass() // now we will get an error that we need to implement required1(arg1) method

Multi interface implementation

const Interface = require('es6-interface');
const testInterface1 = new Set(['required1(arg1)']);
const testInterface2 = new Set(['required2(arg1,arg2)', 'required3({arg1 , arg2 , arg3})']);

class testClass extends Interface(testInterface1,testInterface2) {
  constructor() {
    super()
  }

}

new testClass() // now we will get an error that we need to implement required1(arg1) required2(arg1,arg2) required3({arg1,arg2,arg3}) methods

Multi interface with class inheritance

const Interface = require('es6-interface');
const testInterface1 = new Set(['required1(arg1)']);
const testInterface2 = new Set(['required2(arg1,arg2)', 'required3({arg1 , arg2 , arg3})']);
const testInterface4 = new Set(['required4({arg1, arg2, arg3}, arg4)']);

class parentClass {
    constructor() {

    }
    
    required4({arg1, arg2, arg3}, arg4) {

    }
}
class testClass extends Interface(testInterface1,testInterface2,testInterface4,parentClass) {
  constructor() {
    super()
  }

}

new testClass() //  we will still get an error for no implement required1(arg1) required2(arg1,arg2) required3({arg1,arg2,arg3}) methods as we get required4 from our parent class

Next development tasks

  • improve performance
  • better error messages

Tests

npm test

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.