ES-Class Utility
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
-
ReferenceError: Class is not defined
cause The `es-class` utility was not correctly imported or required into the current scope, or the browser script tag was not loaded.fixIn CommonJS (Node.js), ensure `const Class = require('es-class');` is at the top of your file. In a browser, verify that the `<script>` tag for `es-class.js` is correctly placed and loaded before its usage. -
TypeError: super must be called with 'new'
cause This error can occur if you attempt to call `super()` directly within an `es-class` constructor instead of `this.super()`, or if the context of `this` is lost.fixEnsure that within an `es-class` constructor, you invoke the parent constructor via `this.super(arguments)` or `this.super(param1, param2)`. Also, be mindful of `this` context when passing methods around. -
Uncaught TypeError: Class(...) is not a constructor
cause This error typically occurs if `es-class`'s `Class` function is treated like a native ES6 class (e.g., `new Class(...)`) when it's not set up that way, or if the `Class` function itself hasn't been properly invoked to define the class structure.fixEnsure you are using `Class({...})` to define a class, then `new MyClass()` to create an instance. The error might also suggest an incorrect import/require that doesn't yield the `Class` function itself.
Warnings
- breaking The `es-class` library introduces its own `Class` function and internal concepts like `extends` (as a property) and `super` (as a method). This can lead to significant confusion and potential conflicts when integrated into modern JavaScript codebases that use native ES6 `class` syntax, which has its own keywords and semantics for inheritance and `super` calls.
- deprecated The `es-class` library is largely superseded by native ES6 `class` syntax, which offers standard, performant, and well-supported object-oriented patterns directly in the language. The library's approach to mimicking class features is no longer necessary or recommended for new development.
- gotcha This project appears to be abandoned, with no updates or maintenance for many years. This means it is unlikely to receive security patches, bug fixes, or compatibility updates for newer JavaScript runtimes or browser versions. Using abandoned dependencies introduces security and stability risks.
- gotcha The `implements` feature in `es-class` provides only 'light checks' (runtime warnings if a method is missing) and does not offer true compile-time type safety or interface enforcement like TypeScript interfaces. This can create a false sense of security regarding type contracts.
Install
-
npm install es-class -
yarn add es-class -
pnpm add es-class
Imports
- Class
import { Class } from 'es-class';const Class = require('es-class'); - Class
const Class = require('es-class');// After <script src="//cdnjs.cloudflare.com/ajax/libs/es-class/2.1.1/es-class.js"></script> // Class is available globally.
- Class
import { Class } from 'es-class';import Class from 'es-class';
Quickstart
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());