Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
alamid-class
Advanced tools
Easy prototype inheritance.
alamid-class is a lightweight library (~1.2 kb compressed and gzipped) that allows you to write classes in JavaScript intuitively. It embraces the dynamic and prototypal nature of JavaScript instead of pretending to be a compiled language like Java.
This library is not intended to obscure prototypal inheritance. There are some caveats you should know about if you're new to it.
The syntax is easy to grasp and expressive. A simple class looks like this:
var Class = require("alamid-class");
var Cat = new Class({
name: "Jimmy",
age: 3,
constructor: function (name, age) {
this.name = name || this.name;
this.age = age || this.age;
},
strollAround: function () {
console.log("MEEEOOOWWW!!! Need food! Now!");
}
});
var cat = new Cat();
console.log(cat instanceof Cat); // true
console.log(cat.name); // "Jimmy"
console.log(cat.hasOwnProperty("strollAround")); // false because it is inherited by the prototype
Now you can extend that class with:
var Octocat = Cat.extend({
mood: "sad",
constructor: function () {
this._super("Octocat", 5);
},
strollAround: function () {
console.log("Seeking parents ... but in the meantime:");
this._super();
}
});
The special function this._super()
inside a method provides access to the overridden method. In this case it's simply a shortcut for Cat.prototype.strollAround.call(this)
var octocat = new Octocat();
console.log(octocat instanceof Cat); // true
console.log(octocat.name); // "Octocat"
console.log(octocat.mood); // "sad"
octocat.strollAround(); // "Seeking parents ... but in the meantime:
// MEEEOOOWWW!!! Need food! Now!"
You can also inherit from existing prototypes just like that:
var MyEventEmitter = Class(EventEmitter).extend({
mute: false,
emit: function () {
if (this.mute) return;
return this._super.apply(this, arguments);
}
});
Additionally all instances provide a [read-only reference](https://github.com/peerigon/alamid-class#Read-only property) called Class
on the function that created the instance:
console.log(cat.Class); // Cat
console.log(octocat.Class); // Octocat
Mixins are always part of a flexible class system. In alamid-class a mixin can be any object which will be merged into the prototype.
var Orphan = {
seekParents: function () {
console.log("No parents found. " + this.name + " is feeling " + this.mood + " now...");
}
};
var Octocat = Cat.extend(Orphan, {
mood: "sad",
constructor: function () {
this._super("Octocat", 5);
}
});
var octocat = new Octocat();
octocat.seekParents(); // "No parents found. Octocat is feeling sad now..."
This way you can easy leverage multiple inheritance:
var Orphan = new Class({});
var Octocat = Cat.extend(Orphan, EventEmitter, {
mood: "sad",
constructor: function () {
this._super("Octocat", 5);
// Be sure to apply the mixin's constructors with the correct arguments manually
Orphan.call(this);
EventEmitter.call(this);
}
});
var octocat = new Octocat();
octocat.emit("seeking parents");
octocat.seekParents(); // "No parents found. Octocat is feeling sad now..."
If two mixins define a property with the same name, the latter mixin will simply override the former. This especially applies to the constructor
, so be sure to call it manually.
A class itself can also augment existing objects:
var someObj = {};
Octocat.mixin(someObj);
Octocat.call(someObj); // Applying the constructor manually
someObj.seekParents(); // "No parents found. Jimmy is feeling sad now..."
For a nicer debugging experience and better stack traces you can give your classes names. Take a look at the difference:
But in order to set the function's name alamid-class needs to use eval(). Since eval() is slow and you usually don't need class names in production, this feature is only available when Class.dev = true
. In production mode, names are simply ignored.
You can set a name like this:
var Cat = new Class("Cat", {...});
var Octocat = Cat.extend("Octocat", {...});
var Class = require("alamid-class");
Creates a new function that will use the given prototypes.
name (optional):
Specifies the name of the returned function. Works only in dev-mode. Defaults to "AnonymousClass"
proto1, proto2, ...:
Multiple prototypes that will be merged into one prototype (while the latter prototype overrides the former). If the given prototype is typeof function, its prototype is used instead. So, passing func
or func.prototype
is the same.
Creates a new function that will inherit from class
and implement the given prototypes.
Copies all properties of class.prototype
to the given target. Returns class
.
Boolean variable that switches alamid-class to dev-mode. Defaults to false. Checkout the example.
Since object and arrays are copied by reference in JavaScript all instances will share the same object as property. Imagine this class:
var MyClass = new Class({
myObj: {}
});
var a = new MyClass();
var b = new MyClass();
console.log(a.myObj === b.myObj); // true
This is a common misconception when using prototypal inheritance. So, if you want an object for each instance you should create it within the constructor like this:
var MyClass = new Class({
myObj: null,
constructor: function () {
this.myObj = {};
}
});
var a = new MyClass();
var b = new MyClass();
console.log(a.myObj === b.myObj); // false
While this is basically accomplished with John Resig's trick, it has been tweaked so the function's length
-attribute isn't modified.
There is one problem with Resig's technique: If the call on this._super() is asynchronous, this._super may point to another function. So, instead of this:
somethingAsync: function () {
var self = this;
setTimeout(function () {
self._super(); // this might point to another function than the overridden
}, 0);
}
you should do:
somethingAsync: function () {
var self = this,
super = this._super;
setTimeout(function () {
super.call(self);
}, 0);
}
Class
Every instance provides a read-only reference called Class
to the function that created the instance. Some could argue that alamid-class should use the built-in constructor
-property for that. The problem is, that constructor
always points to the topmost function in the prototype-chain:
function A() {}
function B() {}
B.prototype = Object.create(A.prototype);
console.log(new B().constructor === A); // true
console.log(new B().constructor === B); // false
That's the reason why alamid-class introduces a new read-only reference to the function that has been called by new
.
alamid-class has been extracted as standalone library from the application framework alamid.
Suggestions and bug-fixes are always appreciated. Don't hesitate to create an issue or pull-request. All contributed code should pass
npm test
npm run test-browser
and then visiting http://localhost:8080/bundle
FAQs
Easy prototype inheritance
The npm package alamid-class receives a total of 7 weekly downloads. As such, alamid-class popularity was classified as not popular.
We found that alamid-class demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers 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.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.