Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
The minimalistic class and inheritance utility for javascript.
clutility is a minimal class declaration system that supports just a few concepts:
More details can be found below. Show me the code first! How do I use it?
After adding clutility to your project (see below for instructions) you are ready to use clutility.
// Animal is a constructor function, which prototype provides the 'makeSound' and 'printSound' methods.
var Animal = clutility({
sound : null, // (optional) field declaration
//The code that is required to initialize a new instance of this class, should always be put in the 'initialize' function
initialize : function(sound) {
this.sound = sound;
},
makeSound : function() {
return this.sound;
},
printSound : function() {
console.log(this.makeSound());
}
});
//Dog extends the Animal class
var Dog = clutility(Animal, {
//Dog has its own initializer. But, the initializer of Animal can be called by using $super()
initialize : function($super, sound) {
sound += "!!!!";
//$super can be called at any convenient time, or not at all..
$super(sound);
},
//$super can be used to invoke any overriden class method
printSound : function($super){
$super();
$super();
}
});
//Cat extends the Animal class as well
var Cat = clutility(Animal, {
initialize : function($super) {
$super("meowWWW");
},
//It is not mandatory to have a $super call
makeSound : function() {
return "__" + this.sound + "__!";
}
});
var dog = new Dog("wraf");
dog.printSound();
// prints 'wraf!!!!'
// prints 'wraf!!!!'
var cat = new Cat();
cat.printSound();
// prints '__meowWWW__'
So, to summarize, there are only three functions to remember:
clutility(superclass?, properties)
returns a new constructor function and sets up the prototype (chain)initialize
. If a function with this name is defined under properties, it will be used as constructor to set up the properties of a new instance of this class.$super(args?)
. If $super
is the name of the first argument of any of the function of the new class, $super(args?)
can be used to invoke the super method that is hidden by the current method.bower install clutility
Install the npm package to your project:
npm install clutility --save
Import the class declare function and use it:
var clutility = require("clutility");
var MyClass = clutility({
initialize : function() {
//etc etc
}
})
In your html page:
<script src="clutility.js" />
Why is there yet another class defining system for JavaScript? Well, to be honest, clutility hardly offers any new capabilities. Yet, writing this library was no accident. I needed a class definition system but I couldn't find one that fulfilled all of my needs combined. So, what where my requirements and why did I write a new lib?
In my opinion, a class declaration system should satisfy the following requirements, and thats how clutility was designed:
clutility
, initialize
, and $super
.this.superclass.mymethodname.apply(this, arguments)
are to hard to remember. In clutility you can just call $super()
in any overriding method.new
and instanceof
keywords and typical constructor functions'Private' members can be created by capturing functions and variables in the scope of the class definition. To make this straight forward, instead of providing an object as class definition, you can also pass a function that returns the definition. This function will be called with the prototype of the superclass as first argument. Example:
var Sheep = clutility({
initialize : function(name){
this.name = name;
},
meet : function(other) {
if (other.eatSheep) //Note that Wolf.eatSheep isn't visible
console.log("Hit the road " + other.name + ", and never come back!");
if (other instanceof Sheep)
console.log("Hi " + other.name + ". Meehh..");
}
});
var Wolf = clutility(Sheep, function(zuper) {
function eatSheep(wolf, sheep) {
zuper.meet.call(wolf, sheep); //of course, we could also have passed $super
console.log("You taste delightful, " + sheep.name);
}
return {
meet : function(other) {
eatSheep(this, other);
}
};
});
var blackie = new Sheep("Blackie");
var jack = new Wolf("Jack");
blackie.meet(jack);
//prints 'Hi Jack. Meehh..'
jack.meet(blackie);
//prints 'Hi Blackie. Meehh..'
//prints 'You taste delightful, Blackie'
clutility has no special support for static members. It is recommended to use Javascript best practice here: add static members directly to the constructor function. For example:
var Cat = clutility(Animal, {});
Cat.base_amount_of_lives = 7;
All members defined in the properties of a class are shared among all the instances of that class. This works as expected for function members and primitive members. However for members which are initialized as objects or arrays, please note that, if those members do not get reassigned by the initializer, those will be shared across all instances! This is a typical pattern of javascript's prototype system, but it might have unintended side effects. Example
var EventListener = clutility({
subscriptions : [],
nrOfSubscriptions : 0,
addSubscription : function(item) {
this.subscriptions.push(item);
this.nrOfSubscriptions += 1;
}
});
var e1 = new EventListener();
e1.addSubscription("something");
var e2 = new EventListener();
console.log(e2.nrOfSubscriptions);
//prints '0' (correct, the primitive is overwritten)
console.log(e2.subscriptions);
//prints '["something"]'. Because the member 'subscriptions' never gets reassigned, it is shared through the prototype with all the instances of the class. This might or might not be the intended behavior.
//a proper solutions for this would read:
var EventListener = clutility({
subscriptions : null,
initialize : function() {
this.subscriptions = []; //get a fresh array
}
//rest of the previous implementation
})
clutility has no built in support for mixins to keep the tool as simple as possible. Yet, it is still easy to mixin another class by extending the prototype manually or by using the extend
function of your favorite library (jQuery, underscorejs or lodash). For example:
var MyClass = clutility({ /* properties */});
jQuery.extend(MyClass.prototype, OtherClass.prototype);
Or you could even extend the definition before passing it to clutility.
FAQs
The minimalistic class and inheritance utility for javascript
The npm package clutility receives a total of 0 weekly downloads. As such, clutility popularity was classified as not popular.
We found that clutility 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.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.