
Security News
npm Introduces minimumReleaseAge and Bulk OIDC Configuration
npm rolls out a package release cooldown and scalable trusted publishing updates as ecosystem adoption of install safeguards grows.
extends and constructor plus other featuresFeel free to test all features related to this project, and please come back if your browser is not green.
npm install es-class
It is suggested to install this globally as it's a very small script.
Many thanks to cdnjs for hosting this script. Following an example on how to include it.
<script
src="//cdnjs.cloudflare.com/ajax/libs/es-class/1.2.2/es-class.js"
>/* ES-Class */</script>
Following a list of tested browsers split in Desktop and Mobile, plus a list of server/micro-controller side supported engines.
If you actually know a hybrid (like Espruino) or ES3+ engine that does not work, please file a bug, thank you!
All features explained in the dedicated page.
Following a summary:
1.3.0This is an example of what's possible:
var Engineer = Class({
extends: Person,
with: [
eventEmitter,
growingEachYear,
carrierPath
],
implements: [
iWorker
],
static: {
SOFTWARE: 0,
CONSTRUCTIONS: 1
},
constructor: function (name, age, type) {
this.super(name, age);
this.type = type;
}
});
var me = new Engineer(
'Mr. Andrea Giammarchi',
36,
Engineer.SOFTWARE
);
me instanceof Person; // true
me.type; // Engineer.SOFTWARE
me.emit('work:start', {
tasks: ['do this', 'do that']
});
Using Babel it is possible to make your code directly compatible down to ES5 or even ES3 without loosing the ability to debug in every platform without needing source-map. What you see is basically what you get.
// how you would write in native ES6
class B extends A {
method(x, y) {
return super.method(x, y);
}
get value() {
return super.value;
}
set value(value) {
super.value = value;
}
}
// how you would write in es-class
var B = Class({ extends: A,
method(x, y) {
return this.super(x, y);
},
get value() {
return this.super();
},
set value(value) {
return this.super(value);
}
});
A simple call to babel --whitelist=es6.arrowFunctions,es6.properties.shorthand f.js and the output will be way cleaner than any automation produced by the same transpiler.
Your output will be more readable and also probably faster at execution time.
Looking at the future, grouped static properties and lightweight traits are also in, giving the ability to compose classes through eventual traits initialization without being on the way after class definition.
var wheels = {
init: function () {
if (this instanceof Car) {
this.frontWheels = 2;
this.rearWheels = 2;
} else {
this.frontWheels = 1;
this.rearWheels =
this instanceof Tricycle ? 2 : 1;
}
},
turn: function (where) {
console.log('turning ' + where);
}
};
var engine = {
hp: 120
};
var Car = Class({
with: [
wheels,
engine
]
});
var c = new Car;
c.turn('left'); // trning left
c.hp; // 80
You can also simply npm install -g es-class and use var Class = require('es-class'); whenever you need it.
FAQs
A future proof, backward compatible, JavaScript class utility.
The npm package es-class receives a total of 13,552 weekly downloads. As such, es-class popularity was classified as popular.
We found that es-class demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
npm rolls out a package release cooldown and scalable trusted publishing updates as ecosystem adoption of install safeguards grows.

Security News
AI agents are writing more code than ever, and that's creating new supply chain risks. Feross joins the Risky Business Podcast to break down what that means for open source security.

Research
/Security News
Socket uncovered four malicious NuGet packages targeting ASP.NET apps, using a typosquatted dropper and localhost proxy to steal Identity data and backdoor apps.