ES-Class Utility

2.1.1 · abandoned · verified Sun Apr 19

es-class is a JavaScript utility designed to provide a class-like structure with features reminiscent of ES6/ES7, such as `extends`, `constructor`, `super`, `with` for mixins, `static` properties, and `implements` for light interface checking. Its primary differentiator was its robust backward compatibility, targeting environments as old as ES3 (Internet Explorer 6, Android 2) while offering a forward-looking syntax during the transition to native ES6 classes. The package's current stable version is 2.1.1. However, development appears to be abandoned, with no new releases or maintenance activities for several years, making it a legacy solution for modern JavaScript development. It aimed to bridge the gap between pre-ES6 JavaScript and the then-upcoming native `class` syntax.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates defining a base `Person` class, an interface `iWorker`, and an `Engineer` class that `extends` `Person` and `implements` `iWorker`, using `static` properties and `super`.

const Class = require('es-class');

const Person = Class({
  constructor: function(name, age) {
    this.name = name;
    this.age = age;
  },
  greet: function() {
    return `Hello, my name is ${this.name} and I am ${this.age} years old.`;
  }
});

const iWorker = {
  work: function() { throw new Error('Not implemented'); }
};

const Engineer = Class({
  extends: Person,
  implements: [iWorker],
  static: {
    SOFTWARE: 0,
    CONSTRUCTIONS: 1
  },
  constructor: function (name, age, type) {
    this.super(name, age);
    this.type = type;
  },
  work: function() {
    return `Engineer ${this.name} is working on type ${this.type}.`;
  }
});

const me = new Engineer(
  'Mr. Andrea Giammarchi',
  36,
  Engineer.SOFTWARE
);

console.log(me instanceof Person); // true
console.log(me.type);              // 0 (Engineer.SOFTWARE)
console.log(me.greet());
console.log(me.work());

view raw JSON →